Introduction To PHP
Introduction To PHP
Introduction to PHP
WEB
SERVER
Gets Page
<HTML>
<?php PHP code ?>
</HTML>
HTTP Request
(url)
Server response
CLIENT
Browser creates
the web page
Hello
<HTML>
<B>Hello</B>
</HTML>
Why PHP?
PHP is
Is an open source.
PHP Block
PHP code block is embedded within the <?
php and ?> tags.
When the server encounters the PHP tags it
switches from the HTML to PHP mode.
There are four different ways to embed the
PHP code
<html>
<head>
<title>My personal Hello World! PHP script</title>
</head>
<body>
<?
PHP tag, allow to insert PHP
echo Hello World!;code. Interpretation by PHP
?>
module will substitute the
</html>
code with code output
Variables (I)
To use or assign variable $ must be present before the name of
the variable
The assign operator is '='
There is no need to declare the type of the variable
the current stored value produces an implicit type-casting of the
variable.
A variable can be used before to be assigned
$A = 1;
$B = 2;
$C = ($A + $B); // Integer sum
$D = $A . $B; // String concatenation
echo $C; // prints 3
echo $D;// prints 12
Variables (II)
Function isset tests if a variable is assigned or not
$A = 1;
if (isset($A))
print A isset
if (!isset($B))
print B is NOT set;
Using $$
$help = hiddenVar;
$$help = hidden Value;
echo $$help; // prints hidden Value
$$help = 10;
$help = $$help * $$help;
echo $help; // print 100
For e.g
Strings (I)
A string is a sequence of chars
$stringTest = this is a sequence of chars;
echo $stringTest[0]; output: t
echo $stringTest; output: this is a sequence of chars
Concatenation
$conc = is .a .composed .string;
echo $conc; // output: is a composed string
$newConc = 'Also $conc '.$conc;
echo $newConc; // output: Also $conc is a composed string
Arrays (I)
Groups a set of variables, every element stored into an array
as an associated key (index to retrieve the element)
$books = array( 0=>php manual,1=>perl manual,2=>C manual);
$books = array( 0=>php manual,perl manual,C manual);
$books = array (php manual,perl manual,C manual);
echo $books[2]; output: C manual
Arrays (II)
Working on an arrays
$books = array( php manual,perl manual,C manual);
Common loop
for ($i=0; $i < count($books); $i++)
print ($i+1).-st book of my library: $books[$i];
each
$books = array( php manual=>1,perl manual=>2,C manual=>3);
while ($item = each( $books )) // Retrieve items one by one
print $item[value].-st book of my library: .$item[key];
// each retrieve an array of two elements with key and value of current element
Arrays (II)
Arrays (III)
Multidimensional arrays
$books = array( array(title=>php manual,editor=>X,author=>A),
array(title=>perl manual,editor=>Y,author=>B),
array(title=>C manual,editor=>Z,author=>C));
Common loop
for ($i=0; $i < count($books); $i++ )
print $i-st book, title: .$books[$i][title]. author: .$books[$i][author].
editor: .$books[$i][editor];
// Add .\n for text new page or .<BR> for HTML new page;
Arrays (cont.)
Sorting Functions
PHP Statements
IF statement
if (<condition>) {
//php code goes here
}
else {
//php code goes here
}
Alternative Syntax
if(<condition>) :
//html code goes here
else :
//html code goes here
endif;
For loop
for($i=0;$i < 10;$++i) {
echo(the value is :. $i);
}
Alternative Syntax
for($i=0;$i < 10;$++i) :
// html code goes here
endfor;
While loop
Do-While loop