160 likes | 343 Views
PHP. Teresa Worner. What is it?. P HP: H ypertext P reprocessor server-side scripting language open source cross-platform compatible with almost all servers . php .php3 . phtml. Installing PHP. http://www.php.net/manual/en/install.php XAMPP MySQL, PHP, Perl C:xampphtdocs
E N D
PHP Teresa Worner
What is it? • PHP: Hypertext Preprocessor • server-side scripting language • open source • cross-platform • compatible with almost all servers • .php .php3 .phtml
Installing PHP • http://www.php.net/manual/en/install.php • XAMPP • MySQL, PHP, Perl • C:\xampp\htdocs • http://localhost/
Embedding in HTML • begin/end with tags <?php ... ?> • PHP parser ignores code outside <?php ?> • PHP code run on server, result sent to client as HTML http://www.php.net/manual/en/language.basic-syntax.phpmode.php
Basic Syntax • semicolons • comments // single line comment # also a single line comment /* multi-line comment */ **Note: single line comments end at the end of the line OR at the closing PHP tag – whichever comes first!
Basic Syntax Cont’d • if statements if ($var) //one line elseif ($var2) //one line else //one line • print to screen echo “text”; if ($var) { //multi-line} else { //multi-line} if ($var): //multi-line else: //multi-line endif;
Variables • $variableName • after $, must begin with letter or underscore, then any length of numbers/letters/underscores • predefined variables • examples: $_POST[“name”] $_GET[“name”] $_FILES[“name”]
Constants • define(“constName”, assignment) • do not begin constName with $ • bool, int, float, string
Types //loosely typed $var1 =“0”; $var1 += 2; //$var1 is now an integer of value 2 $var1 += 1.3; //$var1 is now a float of value 3.3 $var1 += “10 people”; //$var1 is float of value 13.3 $var1 += “hi”; //$var1 is float of value 13.3 $var2 = array(key => value, key2 => value2, );
Operators • arithmetic, assignment, bitwise like C++ • comparison $a == $b //true if $a equals $b after type juggling $a === $b //true if $a equals $b and same type $a != $b //not equal after type juggling $a <> $b //not equal after type juggling $a !== $b //not equal and not same type //< > <= >=
Operators Cont’d • Logical $a and $b $a && $b $a or $b $a || $b $a xor $b ! $a • string $a . $b //concatenation
Operators Cont’d • array $a + $b //union of $a and $b $a == $b //true if $a and $b have same key/value pairs $a === $b //same key/value pairs, order, and types $a != $b //not equal $a <> $b //not equal $a !== $b //not identical
Functions • many built-in functions: http://www.w3schools.com/php/default.asp • user declared: function funcName($var1, $var2) { return $var1 + $var2; }
Sessions • session_start • starts or resumes session • default setting saves session ID in cookie • $_SESSION • superglobal array for session variables • $_SESSION[“variableName”] • session_unset • frees session variables • session_destroy • destroys session but does not free session variables or delete cookie
Other Stuff • classes class ClassName { ...} • try-catch try { ... } catch(Exception $e) { ...}
Useful References • http://www.w3schools.com/php/default.asp • http://www.php.net/manual/en/index.php