Week1 Ita2 Intro To PHP
Week1 Ita2 Intro To PHP
Week 1
PHP & Programming Intro
This week
What is PHP
IDE, server
PHP Syntax: variables, datatypes
Echo, print, quotes
index.html
Browser
INTERNET
USER
SERVER
Client- en server-side
scripting languages
Webserver
Scripting Machine
Browser
INTERNET
USER
index.php
SERVER
Client- en server-side
scripting languages
Webserver
Scripting Machine
Browser
INTERNET
USER
index.php
Database
SERVER
What is PHP?
Netbeans
IDE for PHP development
Also for debugging:
connects to webserver while PHP is
running.
Version control (SVN/GIT/Mercurial)
DEVelopment, TeST, PRoDuction.
Netbeans dir, xampp,athena
(php in
(html in
Variables
All variables start with $
Names are case sensitive
Names can contain letters, digits or '_'
Names can not begin with a number
Variables can be declared in advance
Constants:
No $, just capitals
Define(MY_CONSTANT , 23);
Naming convention
variables
(Zend framework) standard
conventions:
Use camelCasing;
$someString, $aLongVariable
myFunctie
Variable names
Valid or not?
variabele2
$variabele3
$4square
$the website
$the_website
Variable names
Valid or not?
variabele2
$variabele3
$4square
$the website
$the_website
Quotes
<?php
$wer =World2"; // declaring variable $wer
echo
echo
echo
echo
echo
echo
echo
echo
?>
"Hello world<br>\n";
'Hello world<br>\n';
"Hallo 'world'<br>\n";
'Hello "world"<br>';
"Hello \"world\"<br>\n";
'Hello \'world\'<br>';
"Hello $wer<br>\n";
'Hello $wer<br>\n';
Expressions
Like C#:
$a = $b + $c;
$b++
$c = $a / $d; (whats the type here?)
Operators
<?php
echo "apple" . cake"; // result: applecake
?>
Assignment operators
Assigning a value to a variable.
$studCount = 8;
$shoeColor = yellow";
Comparison operators
Comparing 2 values (or variables) returns TRUE or
FALSE.
<
smaller than
<=
smaller than or equal to
>
bigger than
>=
bigger than or equal to
==
equal to
!=
not equal to / unequal to
=== identical to (equal to AND the same datatype)
!== not identical to (not equal to OR not the same
datatype)
Comparison operators
$a = 3;
$a
$a
$a
$a
$a
== $b;
== $c;
!= $c;
<= $c;
=== $b;
//
//
//
//
$b = "3";
$c = 5;
// is equal to (TRUE)
is equal to (FALSE)
is unequal to (TRUE)
is smaller or equal to (TRUE)
is equal to, not datatype (FALSE)
Selection: IF ELSEIF
ELSE
selection: IF watch carefully for curly
braces
$amount = 6.98;
if ( $amount <= 6.0 ) {
echo this product is not expensive";
}
elseif ( $amount <= 8.0 ) {
echo this product has an average price";
}
else {
echo this product is too expensive";
}
Selection: Switch
Mostly used for choosing among
alternatives
$language ="en";
switch ($language) {
case "en":
echo "English";
break;
case "nl":
echo Dutch";
break;
case "de":
echo German";
break;
default:
echo No language selected";
}
Iteration: While
As long as condition is TRUE, you execute
something.
while( condition ) {
commands;
}
$i = 0;
while( $i < 10 )
{
echo "\$i is now $i <br>";
$i++;
// i +1
}
Iteration: FOR
Looks like while , mostly used if amount of
loops is known.
for (initial expression; condition; loop-end
expression) {
commands;
}
for ( $i = 0; $i < 10; $i++ )
{
echo "\$i is now $i <br>";
}
Study
W3schools:
variables, echo/print, datatypes,
constants,
operators,if..else...elsif,switch,while,for.
OR (if you have the book):
Beginning PHP 5.3: chapter 3 & 4 +
chapter 1 & 2 (practical)