Académique Documents
Professionnel Documents
Culture Documents
What is PHP?
Why PHP?
z PHP runs on different platforms (Windows, Linux, Unix, etc.)
z PHP is compatible with almost all servers used today (Apache, IIS, etc.)
z PHP is FREE to download from the official PHP resource: www.php.net
1
6/28/2010
PHP Installation
z Download PHP
Download PHP for free here: http://www.php.net/downloads.php
z Download MySQL Database
Download MySQL for free here: http://www.mysql.com/downloads/index.html
z Download Apache Server
Download Apache for free here: http://httpd.apache.org/download.cgi
Configuration of PHP
Install
sta WAMP o on window
do oor LAMP o
on Linux
u pplatform
at o a and
d you do
don’tt need
eed a
any
y
configuration.
OR
2
6/28/2010
PHP Syntax
z <?php
--------
--------
?>
z <html><body>
<?php
echo "Hello World";
?>
</body></html>
Comments in PHP
z <html><body>
<?php
? h
//This is a comment
/*
This is
a comment
block
*/
?>
</body></html>
3
6/28/2010
PHP Variables
z All variables in PHP start with a $ sign symbol.
$var_name = value;
<?php
$txt="Hello World!";
$x=16;
?>
z PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
You do not have to tell PHP which data type the variable is.
In PHP, the variable is declared automatically when you use it.
6/28/2010 Hassan Khan, PUCIT 7
4
6/28/2010
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body></html>
PHP Arrays
In PHP, there are three kind of arrays:
z Numeric array
y - An arrayy with a numeric index
5
6/28/2010
Numeric Arrays
z A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the index
starts at 0):
$cars=array("Saab","Volvo","BMW","Toyota");
Associative Arrays
z An associative array, each ID key is associated with a value.
There are two methods to create a numeric array.
z $ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
6
6/28/2010
Multidimensional Arrays
z $families = array z Array
( (
"Griffin"=>array [Griffin] => Array
( (
"Peter", [[0]] => Peter
"Lois"
Lois , [1] => Lois
"Megan" [2] => Megan
), )
"Quagmire"=>array [Quagmire] => Array
( (
"Glenn" [0] => Glenn
), )
"Brown"=>array [Brown] => Array
( (
"Cleveland", [0] => Cleveland
"Loretta", [[1]] => Loretta
"Junior"
Junior [2] => Junior
) )
); )
$families['Griffin'][2] = ?
6/28/2010 Hassan Khan, PUCIT 13
PHP Looping
z The while Loop
while($i<=5)
{ echo "The number is " . $i . "<br />“; $i++; }
for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />“ }
z The foreach Loop (The foreach loop is used to loop through arrays.)
$x=array("one","two","three");
foreach ($x as $value) { echo $value . "<br />"; }
7
6/28/2010
PHP Functions
z A function will be executed by a call to the function.
z You may call a function from anywhere within a page.
y
<html><body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
?>
</body></html>
6/28/2010 Hassan Khan, PUCIT 15