variables demo
\n"; ecHo ($w/$x + $y*$z) . "
\n"; // echo 12.5, frequently used command Echo ($x > $y) . "
\n"; // false is not echoed; "." is a concat symbol ECHO ($x < $y && $z == FOUR) . "

\n"; // echo 1 (true) // PHP commands are NOT case sensitive but variable names are... // to avoid confusion, please just use all lowercase echo "What is this: [$notexistent]?
\n"; // nothing is printed $w = 'changed to string'; // PHP is not a strongly typed language, like JS echo $w . "
\n"; print ($z%3); // print is similar to echo, % is modulo, prints out 1 echo "

if-else and (cond ? true : false) demo
\n"; if ($x > $y) echo "x is bigger than y
\n"; else echo "x is smaller than y
\n"; echo "x is " . ($x > $y ? "bigger" : "smaller") . " than y

\n"; echo "

switch demo
\n"; switch ($x) { case 1: echo "x is 1
\n"; break; case 2: echo "x is 2
\n"; break; default: echo "x is not 1 or 2
\n"; break; } echo "

\n"; echo "

for loop demo
\n"; for ($i = $x; $i <= $z; $i++) // $x = 2, $z = 4, loop from 2 to 4 echo "i is " . $i . "
\n"; // echo "i is $i
"; // is also possible, notice double quotes echo "

\n

while loop demo
\n"; $i = 0; // $y = 3 while ($i < $y) // 0, 1, 2 and then stop echo "i is " . ($i++) . "
\n"; echo "

\n

do-while loop demo
\n"; $i = 0; // $y = 3 (different behavior with above if $y = 0) do echo "i is " . ($i++) . "
\n"; while ($i < $y); // 0, 1, 2 and then stop (notice the ;) echo "

\n"; echo "

more complex control flow demo
\n"; $a = array(2, 1, 7, 8, 3); // we will discuss array in more details soon for ($i = $sum = 0; $i < count($a); $i++) // count: to count array length if ($a[$i]%2 == 0) continue; // an even number, skip this iteration else if ($sum > 7) break; // stop the associated loop else $sum += $a[$i]; echo "the sum is $sum
\n"; $sum = 0; foreach ($a as $key => $value) { // foreach loop if ($sum > 7) break; $sum += $value%2 == 0 ? 0 : $value; } echo "the sum is $sum

\n"; echo "

function demo
\n"; require_once("sum.php"); // this is like C/C++ #include (next slide) $a = array(2, 1, 7, 8, 3); echo "sum: " . sum($a) . "
\n"; // 21 function fib($n = 7) { // recursive function, default argument $n = 7 if ($n <= 1) return $n; else return fib($n-1) + fib($n-2); } echo "fib(6) = " . fib(6) . "
\n"; // 8 echo "fib() = " . fib() . "

\n"; // default argument, fib(7) = 13 // PHP 1D array demonstration $C = array(13, 1, 12, 5, 7); array_splice($C, 2, 2, array(8, 4, 6)); // replace [12, 5] with [8, 4, 6] $l = count($C); // length of array is now 6: [13, 1, 8, 4, 6, 7] echo $C[5] . "
\n"; // 7 sort($C); // sort ascending, already compare by value echo "sorted: "; foreach ($C as $val) echo $val . " "; // manual echo "
\n"; echo "compare with: " . implode(" ", $C) . "
\n"; // use implode $D = array("N"=>2, "M"=>3); // PHP associative array ~ an ordered map echo "Using var_dump:
\n"; var_dump($D); echo "
\n"; // PHP 2D array demonstration $G = array( array(1, 2), array(0), array(0) ); for ($i = 0; $i < count($G); $i++) { echo "row $i: "; for ($j = 0; $j < count($G[$i]); $j++) echo $G[$i][$j] . " "; echo "
\n"; } echo "Using var_dump:
\n"; var_dump($G); echo "
\n"; // PHP string manipulation demonstration $str1 = 'steven'; echo strlen($str1) . "
\n"; // should be 6 echo $str1[1] . $str1[3] . "
\n"; // 'tv', 0-based indexing $str2 = 'seven'; echo "strcmp: " . strcmp($str1, $str2) . "
\n"; // positive echo "eve: " . strpos($str1, "eve") . "
\n"; // index 2 echo "ev3: " . strpos($str1, "ev3") . "
\n"; // false/not found $seven = 7; echo "$seven
\n"; // will output: 7, notice the difference? echo '$seven
\n'; // will output: $seven // for self testing, not shown to studenets //var_dump($str1); //print_r($G); ?>