1 / 23

PHP Review

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"; ?>.

eugene
Download Presentation

PHP Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Review CSCI 215

  2. A basic file • Create a php file that displays a “hello” message. <?php echo 'Hello'; ?>

  3. 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; ?>

  4. 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?

  5. 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?

  6. Decision Logic • If a is more than b, print “a is greater”. <?php $a = 5; $b = 2; if($a > $b) { echo 'a is greater'; } ?>

  7. 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?

  8. 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; ?>

  9. 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?

  10. 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'; } ?>

  11. 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?

  12. 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?

  13. 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?

  14. Arrays • Add a name to the array. <?php $names[] = 'Khan'; ?> What happens if we omit the square brackets?

  15. 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/>'; } ?>

  16. Arrays & Foreach Loops • Print the array of names using a foreach loop. <?php foreach ($names as $name) { echo $name . '<br/>'; } ?>

  17. 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 ); ?>

  18. Associative Arrays • Add a product and price to the array. <?php $products['gadget'] = 0.99; ?>

  19. 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?

  20. Include • Write a statement to include a file called header.htm <?php include 'header.htm'; ?> What is the difference between include and require?

  21. Functions • Define a function that prints Hello. • Call the function. <?php function hello() { echo 'Hello!'; } hello(); ?>

  22. 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?

  23. 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'); ?>

More Related