CS174
Chris Pollett
Sep 20, 2010
Which of the following statements is true?
@media print { body { font-size: 14pt; } }
<p> A paragraph. Let's hop into interpretive mode: <?php print "hi there"; ?> 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"; //switch case: switch($a) { case 5: echo "hello"; }
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 word is not flat. <?php endif; ?>
$a = array("hi", 1, 2);
$b = array("hi", array(1,2), 2); $c = array("a" => "c");
echo $a[0]." ".c['a'];
foreach($arr as $var){echo $var;} foreach($assoc_arr as $key => $value){echo "Key $key Value $value";}
$arr = array( 0=> 1, 1=>2, 2=>3);
$arr = array("joe"=> 5, "mary" =>6);
$keys = array_keys($arr) and $values = array_values($arr);
$barr[1] = 5; // creates array $barr if doesn't exist
$str="this is a string"; $words = explode(" ", $str); /*acts like split except here the first argument is a string rather than a regular expression. So words is an array("this", "is", "a", "string"). PHP has a split function but not as fast, since arg might be a regular expression. */ $str2 = implode(" ", $words); //undoes the explode.
$cities = array("San Jose", "San Diego"); echo current($cities); // prints San Jose $another = next($cities); // $another is now San Diego;
$cities = array("San Jose", "San Diego"); echo current($cities); // prints San Jose $another = next($cities); // $another is now San Diego;
$lows = array("Mon" => 23, "Tue" => 18); foreach($lows as $day =>$temp ) {echo "$day lows were $temp";}
function name([parameter]){...}
function inc($i){return ++$i;}
$b = inc($a); // leaves the value of $a unchanged
$b =inc(&$a); //here the value of $a is changed (one is added to it).
function inc(&$i){...}