230 likes | 338 Views
PHP Review. CSCI 215. A basic file. Create a php file that displays a “hello” message. <? php echo 'Hello'; ?>. Variables. Define a variable that contains your name. Print “Hello” and your name. <? php $name = 'Tina '; echo "Hello , $ name"; ?>.
E N D
PHP Review CSCI 215
A basic file • Create a php file that displays a “hello” message. <?php echo 'Hello'; ?>
Variables • Define a variable that contains your name. • Print “Hello” and your name. <?php $name = 'Tina'; echo "Hello, $name"; ?> Will this work with single quotes? <?php $name = 'Tina'; echo "Hello, {$name}"; ?> <?php $name = 'Tina'; echo 'Hello, ' . $name; ?>
Concatenation • Create a first name and last name variable. • Use concatenation to print both. <?php $fname = 'Tina'; $lname = 'Ostrander'; echo $fname . ' ' . $lname; ?> What happens if we leave out the space?
Arithmetic • Create two variables a and b that contain integers 5 and 2. • Print the sum, difference, product, quotient, and remainder of those variables. <?php $a = 5; $b = 2; echo 'sum: ' . ($a + $b) . '<br/>'; echo 'difference: ' . ($a - $b) . '<br/>'; echo 'product: ' . ($a * $b) . '<br/>'; echo 'quotient: ' . ($a / $b) . '<br/>'; echo 'remainder: ' . ($a % $b) . '<br/>'; ?> What happens if you omit the parentheses?
Decision Logic • If a is more than b, print “a is greater”. <?php $a = 5; $b = 2; if($a > $b) { echo 'a is greater'; } ?>
Decision Logic • Compare a and b. Print the value of the larger variable. <?php $a = 5; $b = 2; if($a > $b) { echo $a; } else { echo $b; } ?> Are the curly braces required?
Conditional Operator • Compare a and b. Using the conditional operator, print the value of the larger variable. <?php $a = 5; $b = 2; echo $a > $b ? $a : $b; ?>
Switch Statement <?php $quarter = 2; switch ($quarter) { case 1: echo 'Fall'; break; case 2: echo 'Winter'; break; case 3: echo 'Spring'; break; case 4: echo 'Summer'; break; default: echo 'Invalid'; } ?> • Write a switch statement that displays the quarter corresponding to an integer: 1-Fall, 2-Winter, 3-Spring, 4-Summer • Print an error message for any other numbers What happens if you remove the breaks?
Logical Operators • If a does not equal b, and if b is 10 or less, print “YES.” <?php $a = 5; $b = 2; if ($a != $b AND $b <= 10) { echo 'a is greater'; } ?>
For Loops • Write a for loop that prints the numbers from 10 to 1, and then print “Blastoff!” <?php for ($num = 10; $num > 0; $num--) { echo "$num<br/>"; } echo 'Blastoff!'; ?> What happens if we print Blastoff inside the loop?
While Loops • Write a while loop that prints the numbers from 10 to 1, and then print “Blastoff!” <?php $num = 10; while ($num > 0) { echo "$num<br/>"; $num--; } echo 'Blastoff!'; ?> What happens if we forget to decrement the counter?
Arrays • Define an array of names • Print the array without using loop <?php $names = array('Bob', 'Sue', 'Tran'); print_r($names); ?> What are the index numbers of the array elements?
Arrays • Add a name to the array. <?php $names[] = 'Khan'; ?> What happens if we omit the square brackets?
Arrays & For Loops • Print the array of names using a for loop. <?php $count = sizeof($names); for ($i = 0; $i < $count; $i++) { echo $names[$i] . '<br/>'; } ?>
Arrays & Foreach Loops • Print the array of names using a foreach loop. <?php foreach ($names as $name) { echo $name . '<br/>'; } ?>
Associative Arrays • Define an array of three product names with their associated prices. <?php $products = array( 'gizmo' => 3.50, 'whatchamacallit' => 1.25, 'thingamajig' => 4.75 ); ?>
Associative Arrays • Add a product and price to the array. <?php $products['gadget'] = 0.99; ?>
Associative Arrays • Print each item along with its price. <?php foreach ($products as $product => $price) { echo $product . '-'. $price . '<br/>'; } ?> Could we use a for loop here?
Include • Write a statement to include a file called header.htm <?php include 'header.htm'; ?> What is the difference between include and require?
Functions • Define a function that prints Hello. • Call the function. <?php function hello() { echo 'Hello!'; } hello(); ?>
Function Parameters • Define a function that accepts a name and prints Hello, name. • Call the function. <?php function hello2($name) { echo "Hello, {$name}!"; } hello2('Bob'); ?> What happens if we give this function the same name as our previous function?
Function Return Values • Define a function combine that accepts a first name and last name, and returns the concatenation of the two. • Call the function and print the return value. <?php function combine($fname, $lname) { return $fname . ' ' . $lname; } echo combine('Bob', 'Lopez'); ?>