CS174
Chris Pollett
Sep 21, 2016
$bob = 6; echo "Bob=$bob"; // outputs Bob=6 echo 'Bob=$bob'; // outputs Bob=$bob
<p> A paragraph. Let's hop into interpretive mode: <?php print "hi there"; ?> back to copy mode </p>
<p> A paragraph. Let's hop into interpretive mode to echo the variable $foo: <?= $foo ?> back to copy mode </p>
$a = "hello"; //Heredocs $str = <<<EOD This string that lives on multiple lines uses heredocs syntax. $a interpolation happens EOD; echo $str; //Nowdocs $str2 = <<<'EOD2' $a interpolation doesn't happens EOD2; echo $str2;
<?php ob_start(); ?> <p>Hello World</p> <?php $out = ob_get_clean(); $out = strtolower($out); echo $out;
//if if ($a) print "hello"; if ($a) { print "hello"; print "there"; } if ($a) { $b = 1; echo $b; } else if ($b) { $b = 2; echo $b; } else { $c = 1; echo $c; } //switch case: switch($a) { case 5: echo "hello"; /* if $a matches the label (5 in this case) then execute the code after it; */ break; //if hit then jump out of switch case "a label made from a string": echo "weird labels still work"; break; default: echo "if all else fails do this"; }
for($a = 0; $a<10; $a++){echo "hello $a";}
while(!$var) { /*do something*/}
do {/*do something */} while (!$var);
<?php if ($the_world_is_round == true): ?> The world is not flat. <?php endif; ?>
$a = array("hi", 1, 2); //for PHP >= 5.4 you can also use the syntax: $a = ["hi", 1, 2];
$b = ["hi", [1,2], 2]; $c = ["a" => "c"];
echo $a[0]." ".$c['a'];
foreach($arr as $var){echo $var;} foreach($assoc_arr as $key => $value){echo "Key $key Value $value";}
list($first, $second, ) = $arr; //$first will be $arr[0], $second will be $arr[1].