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