PHP By Sergio Rodriguez By Sergio Rodriguez
PHP G PHP: Hypertext Preprocessor G Scripting language G PHP: Hypertext Preprocessor G Scripting language
History G Written by Rasmus Lerdorf Zeev Suraski and Andi Gutmans Rewrote parser PHP 2 G Written by Rasmus Lerdorf Zeev Suraski and Andi Gutmans Rewrote parser PHP 2
History G PHP 3 G PHP 4 G Still being developed G Version Oct G PHP 5 G PHP 3 G PHP 4 G Still being developed G Version Oct G PHP 5
Usage G Server-side Scripting G Web development G Command-line Scripting G Shell scripting G Client-side GUI Apps G Not very common G Server-side Scripting G Web development G Command-line Scripting G Shell scripting G Client-side GUI Apps G Not very common
Server-side Scripting G Main use G Code embedded in HTML to create dynamic web pages G LAMP Architecture G Advantage over others…$$$ …but first! G Main use G Code embedded in HTML to create dynamic web pages G LAMP Architecture G Advantage over others…$$$ …but first!
Syntax G Advantage: Similar to high level PL’s we are familiar with G PHP code goes between tags G G Features that make it different G Advantage: Similar to high level PL’s we are familiar with G PHP code goes between tags G G Features that make it different
Syntax G Embedding HTML in PHP control structures G Will output anything between ?> and the next <?php tags G Embedding HTML in PHP control structures G Will output anything between ?> and the next <?php tags
Types G Type is not explicitly set for variable by programmer G Type of variable is “juggled” throughout PHP code G Criticism due to this (errors not detected at compile time) G Type is not explicitly set for variable by programmer G Type of variable is “juggled” throughout PHP code G Criticism due to this (errors not detected at compile time)
Types Eight different types: G Boolean G Integer G Float AKA Double G String G Array G Object G Resource G Null Eight different types: G Boolean G Integer G Float AKA Double G String G Array G Object G Resource G Null
Boolean G Trivial G Cast as (bool) or (boolean) G False is: G Integer - 0 G Float G String - Empty or “0” G NULL G Array - Size = 0 … among others G True is everything else G Trivial G Cast as (bool) or (boolean) G False is: G Integer - 0 G Float G String - Empty or “0” G NULL G Array - Size = 0 … among others G True is everything else
Integer G No unsigned integers in PHP G Include octal and hexadecimal numbers G Octal - 0[0-7]+ G Hex - 0[xX][0-9a-fA-F]+ G Overflow? Treats value as float G No division operator - yields float G Cast as (int) or (integer) G From float to integer - rounding towards zero G No unsigned integers in PHP G Include octal and hexadecimal numbers G Octal - 0[0-7]+ G Hex - 0[xX][0-9a-fA-F]+ G Overflow? Treats value as float G No division operator - yields float G Cast as (int) or (integer) G From float to integer - rounding towards zero
Float (Double) G Loss of precision when converted to internal binary G Ex: floor(( )*10) is 7, not 8 G Cast as (float), (double), or (real) G Loss of precision when converted to internal binary G Ex: floor(( )*10) is 7, not 8 G Cast as (float), (double), or (real)
String G No bound to size of strings G Single quotes G Escape ‘ or \ characters G Double quotes G More escaped characters (\n, \t, \\, etc) G No bound to size of strings G Single quotes G Escape ‘ or \ characters G Double quotes G More escaped characters (\n, \t, \\, etc)
Array G (key, value) pairs G Key may be an integer index, like Java arrays G print_r($arr); //Prints out array G (key, value) pairs G Key may be an integer index, like Java arrays G print_r($arr); //Prints out array
Object G Use “new” to create new instance G Cast as (object) - new instance of built-in class stdClass G “scalar” member contains value G Use “new” to create new instance G Cast as (object) - new instance of built-in class stdClass G “scalar” member contains value
Resource “ A resource is a special variable, holding a reference to an external resource. ” Taken from the PHP Documentation G i.e. Connectors to DBs (mySQL, MSSQL, etc) “ A resource is a special variable, holding a reference to an external resource. ” Taken from the PHP Documentation G i.e. Connectors to DBs (mySQL, MSSQL, etc)
Variables G Start with $, followed by underscore or letter, and then followed by (number | letter | underscore)* G Dynamic Scoping G Variable variables G Hold variable name as string G Start with $, followed by underscore or letter, and then followed by (number | letter | underscore)* G Dynamic Scoping G Variable variables G Hold variable name as string
Operators G Same as other PL’s G ===, !== G echo `shell command` G. (Yeah, that’s it) G Arrays G +, and equality across key/value pairs G Instanceof (Looks familiar?) G Same as other PL’s G ===, !== G echo `shell command` G. (Yeah, that’s it) G Arrays G +, and equality across key/value pairs G Instanceof (Looks familiar?)
Functions G Again, similar to what we already know G C++-style G i.e. function foo($myVal = 44){/*BODY*/} G return arrays to list G Store function name in variable G Again, similar to what we already know G C++-style G i.e. function foo($myVal = 44){/*BODY*/} G return arrays to list G Store function name in variable
Objects G Nothing new G parent - used in inheritance, refers to base class G PHP 5 is more extensive G Nothing new G parent - used in inheritance, refers to base class G PHP 5 is more extensive
References G Wikipedia - PHP G G PHP Manual G p p G Wikipedia - PHP G G PHP Manual G p p