Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PHP
INTRODUCT ION
Rasmus Lerdorf in 1994 Personal Home Page Tools(94 and 95) for logging PHP/FI(96) Server-side HTML embedded scripting language Introduction of a parser PHP 3.0(98) Zeev Suraski and Andi Gutmans Improved support
7/4/12
Zend engine for each boolean datatype access of form elements user sessions === Java and XML
7/4/12
7/4/12
Portability Open Source Loosely typed language Easy to use Session garbage collection (PHP 5) session.gc_maxlifetime session.gc_probability session.gc_divisor
7/4/12
APACHE
HTML code
www.abc.com/trial.php
PHP Engine
trial.php xyz.html
7/4/12
VARIABLES - What is a variable? RULES 1) A variable name starts with a $ sign, followed by the name of the variable. 2) A variable name must begin with a letter or the underscore character. 3) A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _). 4) A variable name should not contain spaces. 5) Variable names are case sensitive.
7/4/12
$var $this_is_a variable $2name $name2 $_name2 $name| Is $username the same as $Username?
EXAMPL ES
7/4/12
CONSTA NTS
7/4/12
COMMEN TS Shell-style(#)
C++ style(//) C style(/**/)
7/4/12
Integers - is_int, is_integer, notations Floating-point numbers - is_real, is_float, notations Strings - is_string - quotes - escape sequence characters(\,,,$), Booleans - is_bool, false values
COMPOUND TYPES
SPECIAL TYPES
Resource - is_resource, created and used by special functions 7/4/12 - is_null Null
DATATY PES
int, integer bool, boolean float, double, real string array object
7/4/12
7/4/12
++ -- (cast) /*% +< <= >= > == === != <> && || = += -= /= *= %= .= and xor or
OPERATOR PRECEDENCE
7/4/12
if-else switch while do-while for foreach break continue include require goto
CONTROL STRUCTURES
7/4/12
Case-insensitive A function can be declared only once in PHP return statement - How to return multiple values???
7/4/12
Arguments
FUNCTIO NS
7/4/12
Anonymous functions
$anonymous_function = create_function('$a, $b', 'return $a*$b;'); print $anonymous_function(3,7);
Variable Scope
FUNCTIO NS
7/4/12
Alternative Syntax for for: for($i=0;$i < 10;$++i) : // code goes here endfor; Alternative Syntax for if: if(<condition>) : //code goes here else : //code goes here endif;
7/4/12
Variable variables - A variable variable takes the value of a variable and treats that as the name of a variable. $a="hello"; $$a="world"; echo ${$a}; echo $$a;
2.for($i=1; $i<=5; $i++) { for($j=1; $j<=$i; $j++) { echo $j; } echo "<br>"; }
7/4/12
7/4/12
ARRA YS
7/4/12
WORKING WITH time() : Returns the current time, in the number of SECONDS, since Unix DATES epoch(January 1 1970 00:00:00 GMT)
microtime([bool getAsFloat = false]) bool checkdate ( int $month , int $day , int $year ) string date ( string $format [, int $timestamp=time() ] ) array getdate ( [int $timestamp = time()] ) mktime() dateadd() datediff() strtotime()
7/4/12
Key seconds minutes hours mday wday year yday weekday month
Description seconds minutes hours day of the month day of the week year day of the year name of the weekday name of the month
7/4/12
7/4/12
Resources
A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions. Database connect function gives you something by which to identify that connection when you call the query and close functions that is called as a resource. Resources are really integers under the surface. Their main benefit is that they're garbage collected when no longer in use. is_resource( ) function to test whether a value is a resource: if (is_resource($x)) { // $x is a resource }
7/4/12
Objects
PHP supports object-oriented programming (OOP). Classes are the unit of object-oriented design. A class is a definition of a structure that contains properties (variables) and methods (functions). Classes are defined with the class keyword: <?php class Book { var $name = ''; function name ($newname = NULL) { if (! is_null($newname)) { $this->name = $newname; } return $this->name; } }
7/4/12
Objects
Once a class is defined, any number of objects can be made from it with the new keyword, and the properties and methods can be accessed with the -> construct: $b1 = new Book; $b1->name('C'); printf("Book1, %s\n", $b1->name); $b2 = new Book; $b2->name('CPP'); printf("Book2 %s\n", $b2->name); Book1 C Book2 CPP is_object( ) function to test whether a value is an object: if (is_object($x)) { // $x is an object }
7/4/12