1 / 17

Day 3

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.

jade-french
Télécharger la présentation

Day 3

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. Day 3

  2. 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)

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

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

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

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

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

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

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

  10. 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”

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

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

  13. 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(); } }

  14. Programming in PHP (continued) • Operators • Generally the same as C++ • + - * / ++ -- % • Exception: concatenation • Shorthand: • += -= /= *= .= %= $myName = “Thomas”; $myName .= “ Bombach”; // $myName == “Thomas Bombach”; • Comparison operators • == >= <= > < !=

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

  16. 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!”

  17. 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”; */

More Related