PHP5
PHP5
PHP as an Acronym
PHP
GNU? GNUs Not Unix! CYGNUS? CYGNUS is Your GNU Support
HTML
Why do we care?
100% Platform-Independent for the user No additional style or content-arrangement concerns Intelligent application design: How much is done by server? Understand: Goofy animations are done with Javascript, Flash, whatever. PHP is for page content.
Example: html_1.html
<html> <head> <title>My Awesome Site</title> </head> <body> <!-- A Picture --> <img src=me.jpg /> <!-- A Paragraph --> <p>My Sexy Photograph</p> <!-- A Box --> <div style=border-style: dotted> <p> Tack in the Box <br /> Twice! </p> </div> </body> </html>
Example: html_2.html
<html> <head> <title>Coding Contest</title> </head> <body> <!-- A Chart --> <table> <tr> <th> Winner </th> <th> Loser </th> </tr> <tr> <td> Tack </td> <td> Joel </td> </tr> </table> </body> </html>
Variables
<?php // Create a variable $myVariable;
Variables in PHP are Un-typed Declare variables by naming them All variable names start with a dollar sign Standard operations: = + - * / % ++ --
// Do things to variables $augend = 3; $addend = 5; $sum = $addend + $augend; $also_sum = $augend; $also_sum += $addend; $zero = 0; $one = ++$zero;
// Variables can be anything! $integer = 52; $floatvalue = 15.62364; $stringValue = Good Night Nurse; $boolValue = true; ?>
<?php // Example control statements if ( $meal == Steak ) echo Yum!; else echo Blugh!; switch ( $beverage ) { case wine: $BAC = 0.03; break; case jungle juice: $BAC = 0.23; echo Death!; break;
Control Statements All the regulars reappear: if, else if, else, for(), while() Comparison operators: < > == != >= <= ... New comparison: === !== (compares value and type) Compare strings by dictionary order with strcmp()
for ( $i = 1; $i < 5; ++$i ) echo $i; // Special comparison example if ( 1 === true ) echo Big problems.; ?>
Strings Strings are the single most essential core component Concatenation Operator: . When writing a web script, use PHP to output HTML tags, too. Single-Quote Strings are taken 99% Literally Double-Quote Strings will interpolate variables automatically
// Concatenation and assignment $string1 = dog; $string2 = fleas; $uglydog = $string1 . $string2; echo $string1;
// Outputting with tags and // Interpolating variables echo <p> My dog is cute. </p>; echo I said $string1; echo I said $string1; echo I have two {$string1}s; ?>
EXAMPLE 1:
Obfuscated PHP Code!
Arrays in PHP
There are two types of arrays: Numeric and Associative Numeric arrays: Indexed by numbers, starting at 0 Associative arrays: Key-Value pairs PHP denes a special for() loop especially for arrays...
Numeric
Index 0 1 2 ... Value 12345 Fish swim good. [SimpleXML Object] ... Key First Name Last Name Species ...
Associative
Value Hootie Blowsh Salmon ...
<?php // Using Arrays $meals = array( Steak, Cat ); $steak_meal = $meals[0]; $defs = array( Steak => Good, Cat => Dry & Bony ); $review = $defs[$steak_meal];
Arrays and the foreach() Loop Construct arrays with array() The foreach() loop iterates through all dened members of the array. PHP also denes special global arrays, including the ones you see here. Remember: Arrays are just a particular type of variable
// The foreach loop foreach ( $meal as $food ) echo I want $food. <br />; foreach ( $defs as $food => $rev ) echo $food is $rev <br />;
EXAMPLE 2:
What are $_POST and $_GET
Functions
<?php
Functions in PHP, like in Javascript, are very exible and easy to write Variable scope plays an important role (as always) The only tricky thing: default parameters...
function header( $title ) { echo <<<HEADER <html> <head> <title> $title </title> </head> HEADER; } // Another function function multiply( $a, $b ) return $a * $b; } {
These functions come with PHP 5 (One of the big changes) Syntax is modeled after C++ pointer-member access SimpleXML builds an array of each element; we can iterate!
// Start by loading the string or file $xml = simplexml_load_string ($xmlstring); // Now we want to echo everybodys // last name in paragraphs. foreach ($xml->person as $guy) { echo <p>; echo $guy->lastname; echo </p>\n } // Now say we just want the fifth // persons last name: echo <p> FIFTH: ; echo $xml->person[4]; echo </p>\n; ?>
<?php // Connect and Select the database mysql_connect(localhost, admin, mypassword); mysql_select_db(awesome_db); // Now lets grab a name $name = Joel; $result = mysql_query(SELECT age FROM people WHERE name=$name;); $age = mysql_fetch_assoc($result); echo $age[age]; ?>