Introduction To PHP: Prepared By: Fadi Sharif
Introduction To PHP: Prepared By: Fadi Sharif
Introduction To PHP: Prepared By: Fadi Sharif
1 Prepared By:
Fadi Sharif
SERVER SIDE TECHNIQUES
Server side scripting requires installation on the server
side
Typically client sees only html and it unaware that the
html was produced by a server side script
Does not require any installations or add-ons on the
client
2
SERVER SIDE TECHNIQUES
PHP
Java EE: Servlet, JSP
.NET
Ruby
…
3
CLIENT SIDE TECHNIQUES
Requires that the client supports the technique
JavaScript, Applet, Flash, ActiveX…
4
INTRODUCTION TO PHP
PHP stands for HyperText Preprocessor
PHP is a computer scripting language. What does
Scripting language mean?
Originally designed for producing dynamic web
pages
Appeared in 1995
PHP Group is responsible for the language, no
formal specification
Free software
Runs on most operating systems and platforms
URL: http://www.php.net
5
INTRODUCTION TO PHP (cont..)
PHP is a scripting Language: this means the code is
interpreted every time the code is executed
This means that scripting languages are slower than
compiled languages which only requires the code to be
compiled once and the you can run the code without
compiling it every time.
6
WHY PHP?
PHP runs on different platforms (Windows, Linux, Unix, etc.)
PHP is compatible with almost all servers used today (Apache,
IIS, etc.)
PHP is FREE to download from the official PHP resource:
www.php.net
PHP is easy to learn and runs efficiently on the server side
7
PHP INSTALLATION
What do you need?
To create real dynamic web pages using PHP you
will need the following: ( it’s all free)
PHP
Download PHP for free here: http://www.php.net/downloads.php
MySQL Database
Download MySQL for free here:
http://www.mysql.com/downloads/
Apache Web Server
Download Apache for free here:
http://httpd.apache.org/download.cgi
8
PHP INSTALLATION
How to do it the easy way?
Instead of installing PHP, MySQL, Apache separately, you can
install it all as one package and have it all ready and
configured for you.
To do so you need a XAMPP/LAMP package
XAMPP stands for: Windows Apache MySql and
Apache
LAMP, where L Stands for Linux, is designed for Linux
users.
9
PHP INSTALLATION
To install the XAMP server go to
Or for easier installation use XAMPP
http://www.apachefriends.org/en/xampp.html
This package will install for you the following:
PHP 8
MySql
Apache WebServer
PHP my Admin
And more…
10
THREE-TIERED WEB SITE: LAMP/XAMPP
Client example request Server
User-agent: Firefox GET / HTTP/1.1 Apache HTTP Server
Host: www.tamk.fi
User-Agent: Mozilla/5.0 (Mac..)
...
response PHP
Database
MySQL
11
RESPONSE
12
INTRODUCTION TO PHP SYNTAX
PHP has quite easy syntax, if you are familiar
with any C-type language
It has all the same structures that you are
familiar with other programming languages
PHP is designed to output to browser, but it is
possible to do also CLI apps.
13
PHP - SPECIFICS
Delimiters:<?php ?> or just <? ?>
PHP parses code within delimiters
Block comments /* */
Inline comments // #
14
PHP VS. C++
Similarities:
Syntax nearly the same (For/While/If)
Requires semicolons after each statement ;
Assignment is right to left ($num = 56;)
Object-Oriented (Class support, inheritance, virtuals,
polymorphism)
Functions!
Types are nearly the same (booleans, integers, strings,
etc.)
15
PHP VERSUS C++
Differences:
Variables begin with $ sign ($name = “John Doe”;)
No explicit declaration of variable types
Introduction of “lazy” functions (foreach, explode,
mail)
No Function Overloading
“Hidden” functions-within-a-function
Compiled/interpreted during every page load
Documented!
Echo for output
16
EXAMPLE
<?php
$name = “Mohammed”;
echo "Hello " . $name;
?>
17
VARIABLES
Variables in PHP are represented by a dollar sign
PHP supports eight types:
boolean, integer, float, double, array, object, resource and
NULL
18
EXAMPLE (PHP.NET)
<?php
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
21
CONSTANTS
You cannot alter the value of constant after declaration
define(CONSTANT, "value");
print CONSTANT;
22
STRING VARIABLE
String variables are used for values that contain
characters.
After we create a string we can manipulate it. A string
can be used directly in a function or it can be stored in a
variable.
Below, the PHP script assigns the text "Hello World" to a
string variable called $txt:
Example:
<?php
$txt="Hello World";
echo $txt;
?>
23
THE CONCATENATION OPERATOR
<?php
echo strlen("Hello world!");
?>
The output of the code above will be: 12
27
PHP OPERATORS
Assignment Operators
28
PHP OPERATORS
Incrementing/ Decrementing Operators
Dsfdsfdsfds
29
PHP OPERATORS
Comparison Operators
30
PHP OPERATORS
Logical Operators
31
CONTROL STRUCTURES
If, else, elseif, switch
while, do-while, for
foreach
break, continue
32
IF
<?php
if ($a > $b) {
echo "a is bigger than b";
} else {
echo "a is NOT bigger than b";
}
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?> 33
WHILE AND DO-WHILE
<?php
$a=0;
while($a<10){
print $a; $a++;
}
$i = 0;
do {
print $i;
} while ($i > 0);
?> 34
FOR
35
FOR LOOP EXAMPLE
<html>
<head>
</head>
<body>
<?php
for($i=1;$i<=6;$i++){
echo " <br><h" . $i . "> Hello world </h" . $i . ">" ;
}
$name = "ahmed”;
echo "welcome " . $name;
?>
</body>
</html> 36
THE HTML OUTPUT
<html>
<head>
</head>
<body>
</body>
</html>
37
FOREACH
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
echo $value;
}
38
SWITCH
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
} 39
PHP ARRAYS
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with
a value
Multidimensional array - An array containing one or more arrays
40
NUMERIC ARRAYS
Numeric arrays 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");
2. In the following example we assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
41
NUMERIC ARRAYS
Example:
In the following example you access the variable values by
referring to the array name and index:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
The code above will output: Saab and Volvo are Swedish
cars.
42
ASSOCIATIVE ARRAYS
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not
always the best way to do it.
With associative arrays we can use the values as keys and assign values
to them.
Example 1
44
REFERERNCES
www.w3schools.com
www.php.net
45