Professional Documents
Culture Documents
PHP
PHP
Download progam
1 - Download xampp
2 – start Apache
3 – go to path ‘ C/xampp/htdocs’
4 – create folder ex (test)
5 – create file ex (test.php)
6 – open browser
7 – go to ‘ localhost/test/test.php
outline
• Data Types & variables .
• constants .
• echo .
• operators .
• if and else .
• switch .
• loops .
• functions .
• Arrays .
• super global .
• File read/write.
• File Download.
• File Upload.
• Include and Required.
• Sessions.
• Cookies.
• Filters.
• Exception Handling.
• Form Handling and Validation.
• Class / object.
• Constructor.
• Constant.
• Access modifier .
• Inheritance.
• Abstraction.
• Interface.
• Overloading.
• Final .
• Static .
• connect Data Base .
Data Types & Variables
• $var = 5
• $var += 5 $var = $var + 5 $var
= 10
• $var -= 5 $var = $var - 5 $var
= 0
• $var *= 5 $var = $var * 5 $var
= 25
• $var /= 5 $var = $var / 5 $var
= 1
Comparison
5 == 5 5===“5”
true false
5 ==“5“ 5 >= 10
true false
5 != 10 10 > 5
true true
5 <> 5 “ali” < “moahmed”
false true
Inc and Dec
• $var = 5 ;
echo $var++ ;
echo $var ;
• $var = 8 ;
echo ++$var;
echo $var;
• $var =10 ;
echo $var --;
Logical
• and | &&
all conditions true
• or | ||
at least one true
• !
! (true) false
! (false) true
String
• . concatenation
$string= “ali” . “ “ .”moahmed” ;
“ali mohamed”
• .=
$string= “hollow”;
$string .= “ ll “;
$string .=“world”;
or
$string .=“ ll “ . “world”;
“hollow ll world”
Let’s Try
if..else
if ($grade < 50){
echo "F" ;
} elseif ($grade <60){
echo "D" ;
} elseif ($grade <70){
echo "C" ;
} elseif ($grade <80){
echo "B" ;
} else {
echo "A" ;
}
if..else (cont.)
if ($grade < 70){
echo "C" ;
} elseif ($grade <60){
echo "D" ;
} elseif ($grade <50){
echo “D" ;
} elseif ($grade <80){
echo "B" ;
} else {
echo "A" ;
}
switch
switch ($character){
case "A" :
echo "The Character is A ";
break;
case "B" :
echo "The Character is B ";
break;
case "C" :
echo "The Character is C ";
break;
default :
echo "The Character is unknown " ;
break;
}
Loops
• for loop :
for(Initialize ; Condition ; Increment )
{
code ;
}
• Example :
for ($i=0 ; $i<10 ; $i++)
{
echo $i ;
}
Loops(Cont.)
• while loop :
Initialize;
while(Condition)
{
code ;
Increment;
}
Functions
• Function without arguments :
function functionName(){
code ;
}
Example
function printMessage ()
{
echo “message”;
}
Functions(Cont.)
• Function with arguments :
function functionName($arg1 , $arg2){
code ;
}
Example
function factorial($number)
{
$result=1;
for ( $i =1 ; $i <=$number ; $i++)
{
$result *= $i;
}
return $result ;
}
Arrays
• Indexed Arrays :
$arrayName = array(val1 ,val2 ,val3);
Or
$arrayName = [val1 ,val2 ,val3];
0 1 2
$arrayName [ index ];
Arrays(Cont.)
Arrays(Cont.)
• Associative Arrays :
$arrayName = array(key1 => val1,key2 => val2,key3 => val3);
foreach
superglopals
• $GLOBALS
$x = 75;
$y = 25;
function addGlobals() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
Superglopals (cont.)
• $_SERVER
$_SERVER['PHP_SELF'] Returns the filename of the currently executing script
$_SERVER['GATEWAY_INTERFACE'] Returns the version of the Common Gateway Interface (CGI) the
server is using
$_SERVER['SERVER_ADDR'] Returns the IP address of the host server
$_SERVER['SERVER_NAME'] Returns the name of the host server (such as www.w3schools.com)
$_SERVER['SERVER_PROTOCOL'] Returns the name and revision of the information protocol (such as
HTTP/1.1)
$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (such as POST)
• To remove cookies :
Filters
• Filter id from 257 to 275 return Boolean.
• Filter id from 513 to 521 return value.
Result is 155 .
Exception handling
Try and Catch
Used to change the normal flow of the code execution
if a specified error condition occurs .
Form handling
trim — Strip whitespace (or other characters) from the
beginning and end of a string
Form handling
Form handling
class and object
class className
{
attributes .
functions .
}
class Foo
{
public $id = 15 ;
public $name = ‘Ayman‘ ;
function aMemberFunc()
{
print ‘my name is ‘ . $this -> name ;
}
}
$foo = new Foo();
$foo -> amemberFunc(); // prints my name is Ayman
constructor
class Foo
{
public $id ;
public $name ;
function showConstant()
{
echo self::CONSTANT ;
}
}
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
return $template;
}
}
overloading
__call is triggered when invoking inaccessible methods in an object context.
__callStatic() is triggered when invoking inaccessible methods in a static context.
class MethodTest{
public function __call($name, $arguments){
if ($name== "sum“ ){
if (count($arguments)==2){
return $arguments[0]+$arguments[1];
}
else {
return $arguments[0]+$arguments[1]+$arguments[2];
}
}}
public function send() {
echo "string";
}}
$ob = new MethodTest();
echo $ob->sum(1,2,3);
echo $ob->sum(1,2);
$ob->send();
Final
Note for Java developers: the 'final' keyword is not used for class constants in PHP.
We use the keyword 'const'.
class B extends A {
protected static $a; // redefine $a for own use
B::init('lala');
echo 'A::$a = '. A::getA() .'; B::$a = '.B::getA();