250 likes | 390 Views
ALBERT WAVERING BOBBY SENG. CPSP 229D: Web desiGN. Week Whatever: PHP. Announcements/questions/complaints. What is PHP?. Server-side scripting language P HP: H ypertext P rocessor Created in 1995; current version 5.3.3 Dynamically typed (like JavaScript) Other scripting languages:
E N D
ALBERT WAVERING BOBBY SENG CPSP 229D: Web desiGN
Week Whatever: PHP • Announcements/questions/complaints
What is PHP? • Server-side scripting language • PHP: Hypertext Processor • Created in 1995; current version 5.3.3 • Dynamically typed (like JavaScript) • Other scripting languages: • Microsoft ASP • Adobe ColdFusion • Java (using JSP) • Ruby (using Rails)
PHP • Scripts are stored on a server • Interpreted on servers when a page is requested • I request example.php • Server looks up the file • PHP software processes PHP file • Output is HTML+CSS+JavaScript that is returned as the requested page
Real Life Examples • Facebook • ‘Compressed’ PHP • Wikipedia • Photobucket • Yahoo! • YouTube
PHP • Can run on most operating systems • Compatible with most servers (Apache, IIS, etc) • Lets you create dynamic webpages • Most useful when you want to insert specific custom data • Where does this data come from? • Databases!
PHP + MySQL • MySQL is a Structured Query Language database platform • PHP has native built-in support for MySQL • While interpreting a PHP script, PHP software can make multiple requests to multiple MySQL databases • PHP also supports many other databases
PHP Basics • PHP Script can contain: • HTML • CSS • JavaScript • PHP script • PHP files have extension .php • If you save them as .html, the PHP software will not process them • Closed source – your users cannot read your code (unlike JavaScript)
PHP Basics • PHP scripts are written inside a <?php ?> tag • Anything you want interpreted has to be inside these tags • Example: <html><body><?phpecho "Hello World";?></body></html>
PHP Basics • Like Java, every line in PHP ends with a semicolon; • //Single-line comments • /*Multi-linecomments*/
PHP Basics • PHP variable names are always preceded by a $ • Assigning a value to a variable:$variable_name = value; • PHP variables are loosely typed • Don’t have to declare variables before using them • Can reassign variables to hold different types of values
PHP Basics • Variables can only include alphanumeric characters and the underscore • Variables must start with a letter or an underscore • PHP variables have a single scope, usually • Variables in functions are limited to those functions • The keyword global will use global variables inside a function
The global keyword <?php$a = 1;$b = 2;function Sum(){ global $a, $b; $b = $a + $b;} Sum();echo $b;?>
Operators + Addition += - Subtraction -= * Multiplication *= / Division /= % Modulus %= ++ Increment -- Decrement = Assignment .= Concatenate (Strings)
Comparison Operators == Equal to != Not equal to <> < Less than <= Less than or equal to > Greater than >= Greater than or equal to
Logical Operators && and AND || or OR ! not xor XOR
If/elseif/else if (condition) statement; elseif (condition) statement; else statement;
Switch switch(variable){ case a: statement; break; case b: statement; break; default: statement; }
Loops while (condition){ statements; } for (initialization; condition; increment){ statements; }
Arrays • Numerically indexed • Associatively indexed (maps) • Multidimensional (combination of one or any of the above) • Create an array with $var_name = array(); • Index with $var_name[0] = assignment; or $var_name[$other_var] = assignment;
PHP GET • Built-in functions to get data from pages • $_GET function collects values from a form sent with method = get HTML Page: <form action="welcome.php" method="get"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> PHP Response: Welcome <?php echo $_GET["fname"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!
PHP POST • Get is limited to 100 characters • Not awesome • Post has 8Mb maximum • Awesome
PHP Post HTML Page: <form action="welcome.php" method="post"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> PHP Response: Welcome <?php echo $_POST["fname"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old!
Resources • Ask Jeeves • AltaVista • http://php.net/manual/en • Language Reference • Your new best friend • Code examples, user questions, hints • http://www.w3schools.com/php/default.asp
Homework • Create a PHP script that outputs the current date to a page, inserted among other usual HTML elements in a full page • http://php.net/manual/en/function.date.php • Use one additional construct of PHP (array, loop, if/else, etc) • Upload to your account on my server as a .php file by the start of next class • Be creative! • Databases next time (real fun starts)