Você está na página 1de 30

7/4/12

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

INTRODUCT ION PHP 4.0(2000)


Zend engine for each boolean datatype access of form elements user sessions === Java and XML

7/4/12

INTRODUCT ION Applications

Dynamic web pages

Desktop applications(PHP GTK) Online communities, automated newsletters

7/4/12

INTRODUCT ION Why PHP ???


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

INTRODUCT WAMP LAMP MAMP ION


WORKING
Client (Web Browser)
www.abc.com/xyz.html

APACHE
HTML code

www.abc.com/trial.php

PHP Engine

trial.php xyz.html

MySQL WINDOWS 7/4/12

INSTALLING WAMP SERVER

Click to edit Master subtitle style

7/4/12

LEXICAL SYNTAX STRUCTURE <?php


//code; ?>

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

SYNTAX : define(name, value, case_insensitive); E.g. define(PI,3.14);

CONSTA NTS

7/4/12

COMMEN TS Shell-style(#)
C++ style(//) C style(/**/)

7/4/12

DATATY Type Juggling PES


SCALAR TYPES

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

Arrays - indexed and associative, is_array Objects

SPECIAL TYPES

Resource - is_resource, created and used by special functions 7/4/12 - is_null Null

gettype() settype() Casting


-

DATATY PES

int, integer bool, boolean float, double, real string array object

7/4/12

OPERAT ORS ARITHMETIC OPERATORS : +,-,*,/,%,negation(-)


ASSIGNMENT OPERATORS : =,+=,-=,*=, /=, %=, STRING OPERATORS : ., .= INCREMENT/DECREMENT OPERATORS : ++, -COMPARISON OPERATORS : ==, ===, !=, <>, !==, >, <, >=, <= - TERNARY OPERATOR : (condition)?true_statement:false_statement LOGICAL OPERATORS : and, or, xor, &&, ||, ! BITWISE OPERATORS : &, |, ^, ~, <<, >> ARRAY OPERATORS : +, ==, ===, !=, <>, !==

ERROR CONTROL OPERATOR : @

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

FUNCTIO NS SYNTAX for creating a User-Defined Function


function <function_name>(<parameters_if_any>) { //code }

Case-insensitive A function can be declared only once in PHP return statement - How to return multiple values???

7/4/12

Arguments

FUNCTIO NS

By value and by reference(&) Default argument values

Variable length argument lists - func_num_args - func_get_args - func_get_arg(argument_number) Missing parameters

7/4/12

FUNCTIO Variable functions NS function display()


{ Echo Inside function; } $name=display; $name();

Anonymous functions
$anonymous_function = create_function('$a, $b', 'return $a*$b;'); print $anonymous_function(3,7);

Recursive functions E.g. Fibonacci Series


7/4/12

Variable Scope

FUNCTIO NS

Global Variables global and $GLOBALS[variable_name] Static Variables

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;

Assigning variable by reference $a=&$b; By default, variables are assigned by value

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;

1.$number=50; function tenTimes() { $number = $number * 10; } tenTimes(); print $number;

2.for($i=1; $i<=5; $i++) { for($j=1; $j<=$i; $j++) { echo $j; } echo "<br>"; }
7/4/12

7/4/12

Enumerated/Indexed Array Associative Arrays Appending Multidimensional Array

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