CS174
Chris Pollett
Sep 21, 2020
php name_of_script.php #runs name_of_script.php
php -a # run in enter interactive mode. will get a prompt...
php > echo "hello world";
hello world
php > quit
cd ~/public_html
php -S localhost:8000 #starts built-in web server on port 8000
# using current dir as document root
short_open_tag = Onso that you can escape to a PHP script using <? rather than <?php
;error_reporting = E_ALL & ~E_NOTICEand uncomment it, restart Apache
$bob = 6; echo "Bob=$bob"; // outputs Bob=6 echo 'Bob=$bob'; // outputs Bob=$bob
Which of the following statements is true?
<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; ?>