170 likes | 317 Views
Day 3. Review: PHP. PHP: Hypertext Preprocessor Used to create dynamic web pages Can be integrated alongside HTML PHP files have a .php extension PHP scripts must be run on a server with PHP (meaning that you can’t simply view PHP files that are on your local computer). Review: PHP Syntax.
E N D
Review: PHP • PHP: Hypertext Preprocessor • Used to create dynamic web pages • Can be integrated alongside HTML • PHP files have a .php extension • PHP scripts must be run on a server with PHP (meaning that you can’t simply view PHP files that are on your local computer)
Review: PHP Syntax • <?php ?> • Code goes inside the PHP “tag” • Similar to C++ • Variables • Functions • Statements (that end with semicolons) Example: <html> <head></head> <body> <?php echo “Hello, World”; ?> </body> </html>
Review: Outputting text • echo – two ways to use it: echo “Some text”; or echo(“Some text”); • Outputs directly to the HTML document (meaning that you can output HTML tags in PHP) • helloworld.php <?php echo “<html><head></head>”; echo “<body>”; echo “Hello, World”; echo “</body></html>”; ?>
Review: PHP Variables • $ symbol • PHP variables are in the form $variableName • Variable names can contain alphanumeric characters (plus underscores), but cannot start with a number • PHP variables are loosely typed – a variable can change from a string to an integer or character and vice-versa • Best practice: • If you want to choose a variable name that contains spaces (such as “play counter”) the convention is to use underscores (play_counter) or camel-case (playCounter)
Strings • Like C++, strings are variables representing text $name = “Thomas”; $foo = “bar”; • 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
String Functions • PHP has many functions that operate on strings • Functions are bits of code that can be used over and over again • These functions return different values depending on their intended use • Usually in the form: functionName($stringVariable); • Usually we store the output of the function in a variable (since the function doesn’t change the original value) $newVariable = functionName($stringVariable);
String Functions (continued) • 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 • Works with non-variable text too (though why would you want to?) echo strlen(“Thomas”); // Output: 6
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 • If the string does not appear in the larger string, then the function returns false: $word = “onomatopoeia”; $wordPos = strpos($word, “apostrophe”); // $wordPos == false;
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”
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”
str_replace($find, $replace, $string, $count) strlen($stringVariable) strpos($searchWithin, $searchFor) substr($string, $start, $length) Exercise: Decoding a Message • A friend (a famous cryptologist) has given you an secret message: zyxwqhbyxwsqbrravbnsaqmabcighqabcd • He also gave instructions for decoding it: • First, replace any occurrences of “yxw” with “roo” • Next, replace any occurrences of “abc” with “idn” • Replace all ‘q’s with ‘t’s • Replace all ‘b’s with ‘e’s • Remove the first 4 and last 4 characters of the string (probably using substr!) • Finally, replace any occurrences of “ravens” with “crows” • Figure out the message before bad things happen!
Programming in PHP • Control statements • if/else if/else work the same as in C++: if( firstCondition) { } else if( secondCondition ){ } else { } • Best Practice: • Like other languages, PHP supports omitting the curly braces for one-line if/else if/else statements. This is bad practice, as demonstrated by the following code: if (condition) if (other condition) runCode(); else otherCode(); • It is much more preferable to always use curly braces: if(condition) { if(other condition) { someCode(); } else{ otherCode(); } }
Programming in PHP (continued) • Operators • Generally the same as C++ • + - * / ++ -- % • Exception: concatenation • Shorthand: • += -= /= *= .= %= $myName = “Thomas”; $myName .= “ Bombach”; // $myName == “Thomas Bombach”; • Comparison operators • == >= <= > < !=
Programming in PHP • Date object – 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(“Y/m/d”); // Outputs 2009/07/08 • 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) • Print out current time and date in this format: Hours: Minutes: Seconds Month/Day/Year
Project: Different Messages At Times • Use if/else if/else and the date object to echo different messages to visitors • Between 5:01am and 7:00am, output “You’re up early!” • Between 7:01am and 12:00pm, output “Good morning!” • Between 12:01pm and 5:00pm, output “Good afternoon!” • Between 5:01pm and 11:00pm, output “Good evening!” • Otherwise, output “Goodnight!”
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”; */