Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAY 4. Review: Strings Strings can be concatenated (joined together) using the. (period) operator: $fullName = “Thomas ”. “Bombach”; This works when outputting.

Similar presentations


Presentation on theme: "DAY 4. Review: Strings Strings can be concatenated (joined together) using the. (period) operator: $fullName = “Thomas ”. “Bombach”; This works when outputting."— Presentation transcript:

1 DAY 4

2 Review: Strings Strings can be concatenated (joined together) using the. (period) operator: $fullName = “Thomas ”. “Bombach”; This works when outputting text as well: echo “Camp ”. “CAEN”; // Output: Camp CAEN  String variables can also be concatenated: $first = “PHP ”; $second = “rocks”; echo $first. $second; // Output: PHP rocks

3 Review: String Functions  PHP has many functions that operate on strings  Usually we store the output of the function in a variable (since the function doesn’t change the original value) $newVariable = functionName($stringVariable);

4 Review: String Functions  String length: strlen($stringVariable)  Counts the number of characters (including spaces) in a string $name = “Thomas”; $nameLength = strlen($name); // $nameLength is equal to 6 // $name is unchanged

5 Review: String Functions (continued)  String position: strpos($searchWithin, $searchFor)  Finds the first occurrence of a string within a string  Returns the index (position) of the string (like arrays, indexing starts at 0 – the first character in a string is at position 0) $game = “Counterstrike”; $strikePos = strpos($game, “strike”); // $strikePos == 7 $counterPos = strpos($game, “Counter”); // $counterPos == 0

6 Review: String Functions (continued)  Sub strings: substr($string, $start, $length)  Returns a part of the string. $start is the position in the string to start from, and $length is the number of characters to use $word = “hyperbole”; $partial = substr($word, 0, 5); // $partial == “hyper”  If the $length parameter is left out, the sub string goes to the end of the original string $word = “antithesis”; $partial = substr($word, 4); // $partial == “thesis”

7 Review: String Functions (continued)  Replacing parts of a string: str_replace($find, $replace, $string, $count)  $find is the string to replace  $replace is the string to use wherever the $find string is found  $string is the string to be searched  $count is an optional variable that counts the number of replacements $word = “metonymy”; $newString = str_replace(“tony”, “bill”, $word); // $newString == “mebillmy”

8 Review: Programming in PHP  Operator-Assignment Shorthand: += -=/=*=.=%=  Example 1: $myName = “Thomas”; $myName.= “ Bombach”; // $myName == “Thomas Bombach”  Example 2: $number = 6; $number += 5; // $number == 11

9 Review: Date  Date – utility in PHP to get the date and time  Getting the current date: date($format) function $format is a string that contains information on what parts of the date or time to display, based on certain characters echo date(“h:m:s m/d/Y”); // Outputs 01:23:45 07/08/2009 d - The day of the month (from 01 to 31) m - A numeric representation of a month (from 01 to 12) Y - A four digit representation of a year G - 24-hour format of an hour (0 to 23) h - 12-hour format of an hour (01 to 12) i - Minutes with leading zeros (00 to 59) s - Seconds, with leading zeros (00 to 59)

10 PHP Arrays  2 types of arrays  Numeric Conventional arrays, with the keys being numbers: $myArray = array(“First”, “Second”, “Third”); /* $myArray[0] == “First”; $myArray[1] == “Second”; $myArray[2] == “Third”; */  Associative Keys are not numbers, but strings $myArray = array(“first” => “Thomas”, “last” => “Bombach”); /* $myArray[“first”] == “Thomas”; $myArray[“last”] == “Bombach”; */

11 Array Functions  Similar to strings, there are functions that operate on arrays  count($array)  Returns the number of entries in an array  Works on both numeric and associative arrays  Example: $myArray = array(“first”, “second”, “third”); echo count($myArray); // Outputs 3

12 Numeric Arrays  Created with array() syntax  Passing in a series of values creates indexes starting from 0, increasing by 1  Example: $myArray = array(5, 6, 7); // Creates an array of length 3 /* myArray[0] == 5 myArray[1] == 6 myArray[2] == 7 */

13 Numeric Arrays (continued)  Indexes (positions in the array) can also be specifically chosen by passing in key/value pairs  Key/value pairs are in the form key => value  Example: $myArray = array(5 => “a”, 6 => “b”, 19 => “c”); /* $myArray[5] == “a” $myArray[6] == “b” $myArray[19] == “c” */

14 Numeric Arrays (continued)  Accessing numeric arrays  Same as C++  Example: $myArray = array(“first”, “second”, “third”, 42=> “Answer to the Ultimate Question of Life, the Universe, and Everything”); echo myArray[0]; // Outputs: first echo myArray[1]; // Outputs: second echo myArray[2]; // Outputs: third echo myArray[42]; // Outputs: Answer to the Ultimate Question of Life, the Universe and Everything myArray[3] = “fourth”;

15 Looping Over Numeric Arrays  Using a for loop  Same as C++  Example: $myArray = array(0, 1, 1, 2, 3, 5, 8); for($i = 0; $i < 7; $i++) { echo myArray[$i]. “ ”; } /* Outputs: 0 1 2 etc. */

16 Associative Arrays  Uses strings instead of numbers as keys in the array  Example: $employee = array(“title” => “Programmer”, “salaryPerYear” => 80000, “expendable” => true); /* $employee[“title”] == “Programmer” $employee[“salaryPerYear”] == 80000 $employee[“expendable”] == true */

17 Associative Arrays (continued)  Accessing associative arrays  Similar to numeric arrays, use keys  Example: $players = array(“qb” => “Rick Leach”, “wr” => “Anthony Carter”, “rb” => “Tom Harmon”); echo $players[“qb”] // Outputs: Rick Leach echo $players[“wr”] // Outputs: Anthony Carter echo $players[“rb”] // Outputs: Tom Harmon

18 Looping over Associative Arrays  Use the foreach syntax  Different from C++  Example: $players = array(“qb” => “Rick Leach”, “wr” => “Anthony Carter”, “rb” => “Tom Harmon”); foreach ($players as $player) { echo $player. “ ”; } /* Output: Rick Leach Anthony Carter Tom Harmon */

19 Project: Employee Card  In an attempt to find a web-developer job, you’ve decided to put some information about yourself online for employers to find. You decide to make an associative array with all your information, then loop through and print it out, nicely formatted (use CSS & HTML!).  You should include your name, location, skill set, whether you want a full-time or part time position, and any other relevant information  The card should look like this: Name: Thomas BombachLocation: Ann Arbor Skills: Knowledgeable in C++, Java, HTML & XHTML, Javascript (including AJAX), CSS, PHP, MySQL Engineering background Web design experience (using Illustrator, Photoshop, etc.) Desired position: Full-time

20 foreach (continued)  Sometimes you need to use the key name from an associative array  Use the other type of foreach loop: $myArray = array(“idempotent” => “(Adj.) Multiple applications of the operation do not change the result”); foreach($myArray as $key => $value) { echo $key. “: ”. $value. “ ”; } /* Output: idempotent: (Adj.) Multiple applications of the operation do not change the result */

21 Case & Switch  Used instead of if statements to reduce the amount of code  Syntax: switch($variable) { case value1: break; case value2: break; default: }

22 If/else if/else vs. Case & Switch $age = //some number if($age == 65) { /* Run code (flag them as eligible for Medicare) */ } else if($age == 18) { /* Run code (flag them as eligible for the draft) */ } else { // Don’t do anything } $age = //some number switch ($age) { case 65: /* Run code (flag them as eligible for Medicare) */ break; case 18: /* Run code (flag them as eligible for the draft) */ break; default: // Don’t do anything } If/ else if/elseCase & Switch

23 Functions  Similar syntax as Javascript: function functionName() { // Code goes here }  To call a function, you simply use the function name, followed by parenthesis: functionName(); // Calls a function called functionName

24 Functions (continued)  Functions can return variables too whomever called the function  Example: $myName = getName(); function getName() { return “Thomas”; }

25 Functions - Arguments  Arguments (or parameters)  Functions can use variables that are given to them (called arguments). The number of arguments passed to a function must match the number that the function expects  Example: foo(95, “My number: ”); function foo($bar, $desc) { echo $desc. $bar; } /* Output: My number: 95 */

26 Project: Mad Libs  Using associative arrays & functions, make a Mad Libs program  Have different functions for each sentence  Each function should receive only 1 argument, an associative array with keys for nouns, adjectives, etc.  Example: function printSentence($words) { // Use $words[“noun”], $words[“verb”], $words[“noun2”], etc. }

27 Forms & PHP  HTML Form Tag  action is a path to a PHP file that handles the form submission from the user  method specifies how to send the form submission POST GET

28 POST vs. GET  Will not be cached by the browser  Use this method when the request will change the state of data (such as posting a comment on a blog)  Will be cached by the browser  Use this method when the request will not change the data on the server (such as a search) POSTGET


Download ppt "DAY 4. Review: Strings Strings can be concatenated (joined together) using the. (period) operator: $fullName = “Thomas ”. “Bombach”; This works when outputting."

Similar presentations


Ads by Google