Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Programming What we know so far… …and something new.

Similar presentations


Presentation on theme: "PHP Programming What we know so far… …and something new."— Presentation transcript:

1 PHP Programming What we know so far… …and something new

2 Forms Creating a form in html:form Monday/Wednesday Tuesday/Thursday Friday/Sunday Saturday

3 processing forms … <?php // get form selection $day = $_GET['day']; // retrieve from form // check value and select appropriate item if ($day == 1) { $special = 'Chicken in oyster sauce'; } elseif ($day == 2) { $special = 'French onion soup'; } elseif ($day == 3) { $special = 'Pork chops with mashed potatoes and green salad'; } else { $special = 'Fish and chips'; } ?> Today's special is: … cooking.php

4 Rewrite with switch <?php // get form selection $day = $_GET['day']; // check value and select appropriate item switch($day) { case 1: $special = 'Chicken in oyster sauce'; break; case 2: $special = 'French onion soup'; break; case 3: $special = 'Pork chops with mashed potatoes and green salad'; break; default: $special = 'Fish and chips'; } ?>

5 strings Displaying and concatenating: echo 'Items ordered: '. $totalqty. ' ';

6 logic Relational operators: ==, !=, >, >=, <, <= if ($age >= 21) { echo 'Come on in, we have alcohol and music awaiting you!'; } else { echo "You're too young for this club, come back when you're a little older"; } Conditional operators: &&, ||, !, xor if (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0) echo $year.’ is a leap year ’ else echo $year.’ is not a leap year ’

7 Tricky logic A self-testing formself-testing form <?php /* if the "submit" variable does not exist, the form has not been submitted - display initial page */ if (!isset($_POST['submit'])) { ?> " method="post"> Enter your age: <?php } else { /* if the "submit" variable exists, the form has been submitted - look for and process form data */ // display result $age = $_POST['age']; if ($age >= 21) { echo 'Come on in, we have alcohol and music awaiting you!'; } else { echo "You're too young for this club, come back when you're a little older"; } ?>

8 Increments/decrements/ shorthand notation $total++ same as $total = $total + 1 $total-- same as $total = $total - 1 $line.= ‘more’ same as $line = $line.’more’ $x += 5 same as $x = $x + 5 $answer = ($x == 10) same as: if ($x == 10) $answer = true;

9 iteration while (condition is true) { do this } do { do this } while (condition is true); for (startval of counter; condition; update counter) { do this }

10 Drawing tables "> Enter number of rows and columns <?php if (isset($_POST['submit'])) { echo " "; // set variables from form input $rows = $_POST['rows']; $columns = $_POST['columns']; // loop to create rows for ($r = 1; $r <= $rows; $r++) { echo " "; // loop to create columns for ($c = 1; $c <= $columns;$c++) { echo " "; } echo " "; } echo " "; } ?> tables.php

11 File processing File management activities: –Creating –Opening –Writing data to a file –Reading data from a file –Locking –Deleting –Closing –Misc.

12 Omelette recipe Open the file and assign it a file handle.file Interact with the file, via its handle, and extract its contents into a PHP variable. Close the file.

13 Writing to a file <?php // set file to writefile $file = 'tmp/dump.txt'; // open file $fh = fopen($file, 'w') or die('Could not open file!'); // write to file fwrite($fh, "Look, Ma, I wrote a file! ") or die('Could not write to file'); // close file fclose($fh); ?>

14 File existence <?php // if form has not yet been submitted // display input box if (!isset($_POST['file'])) { ?> " method="post"> Enter file path <?php } // else process form input else { // check if file existsfile // display appropriate message if (file_exists($_POST['file'])) { echo 'File exists!'; } else { echo 'File does not exist!'; } ?>

15 Multiple file functionsfunctions is_dir() - returns a Boolean indicating whether the specified path is a directory is_file() - returns a Boolean indicating whether the specified file is a regular file is_link() - returns a Boolean indicating whether the specified file is a symbolic link is_executable() - returns a Boolean indicating whether the specified file is executable is_readable()- returns a Boolean indicating whether the specified file is readable is_writable()- returns a Boolean indicating whether the specified file is writable filesize() - gets size of file filemtime() - gets last modification time of file filamtime() - gets last access time of file fileowner() - gets file owner filegroup() - gets file group fileperms() - gets file permissions filetype() - gets file type

16 Other file functions pp. 71-76: special uses; will learn on a “need to know” basis –feof: end of file –fgets, fgetss, fgetcsv: read a line at a time –readfile, fpassthru: read whole file –fgetc: read a character at a time –unlink: delete a file –rewind, fseek, ftell: inside the file –flock: locking the file

17 arrays Composite, or aggregate, data types –Can hold more than one value using the same variable name Examples –Quarterly earnings –$q_earnings = array(3, 6, 1, 4); 3614 [0] [1] [2] [3]

18 arrays Elements Subscripts or indices How to declare/define How to manipulate elements –Inserting –Reading –Deleting –Displaying –Processing Associative arrays

19 Defining an array <?php // define an array $pasta = array('spaghetti', 'penne', 'macaroni'); ?> -or- <?php // define an array $pasta[0] = 'spaghetti'; $pasta[1] = 'penne'; $pasta[2] = 'macaroni'; ?>

20 Defining an associative array <?php // define an array $menu['breakfast'] = 'bacon and eggs'; $menu['lunch'] = 'roast beef'; $menu['dinner'] = 'lasagna'; ?> -or- <?php // define an array $menu = array(‘breakfast’ => 'bacon and eggs‘, 'lunch' => 'roast beef‘, 'dinner' => 'lasagna‘); ?>

21 Adding/changing elements Adding –$pasta[4] = ‘rigatoni’; or –array_push($pasta, ‘rigatoni’); –$menu[‘snack’] = ‘fruit’; Modifying –Just reassign the element

22 Removing elements Removing from the end <?php // define an array $pasta = array('spaghetti', 'penne', 'macaroni'); print_r($pasta); echo ‘ ’; // remove an element from the end array_pop($pasta); print_r($pasta); ?> pastaarray.php

23 Removing elements Removing from the front <?php // define an array $pasta = array('spaghetti', 'penne', 'macaroni'); // take an element off the top array_shift($pasta); print_r($pasta); ?>

24 Adding elements to front <?php // define an array $pasta = array('spaghetti', 'penne', 'macaroni'); // add an element to the beginning array_unshift($pasta, 'tagliatelle'); print_r($pasta); ?>

25 Strings arrays <?php // define CSV string $str = 'red, blue, green, yellow'; // split into individual words $colors = explode(', ', $str); print_r($colors); ?>

26 arrays strings <?php // define array $colors = array ('red', 'blue', 'green', 'yellow'); // join into single string with 'and' // returns 'red and blue and green and yellow' $str = implode(' and ', $colors); print $str; ?>

27 Review functions array_push array_pop array_shift array_unshift explode Implode Also, sort and rsort!

28 Looping through arrays My favourite bands are: <?php // define array $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'); // loop over it and print array elements for ($x = 0; $x < sizeof($artists); $x++) { echo ' '.$artists[$x]; } ?>

29 A niftier way My favourite bands are: <?php // define array $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'); // loop over it // print array elements foreach ($artists as $a) { echo ' '.$a; } ?>

30 Reading files into arrays


Download ppt "PHP Programming What we know so far… …and something new."

Similar presentations


Ads by Google