Complete Guide: by Kaleem Abbas Jara
Complete Guide: by Kaleem Abbas Jara
Complete guide
PHP
By
Kaleem Abbas Jara
Complete PHP 2 CONTENTS
CONTENTS
CONTENTS...................................................................................................................................................................2
PHP.................................................................................................................................................................................3
ABOUT PHP................................................................................................................................................................3
PHP FILE.....................................................................................................................................................................3
ABOUT MYSQL..........................................................................................................................................................3
PHP INSTALLATION....................................................................................................................................................3
What do you Need?...............................................................................................................................................4
PHP BASIC SYNTAX..................................................................................................................................................4
BASIC SYNTAX............................................................................................................................................................4
COMMENTS IN PHP....................................................................................................................................................4
VARIABLES IN PHP...................................................................................................................................................5
VARIABLE...................................................................................................................................................................5
Variable declaration.............................................................................................................................................5
Naming Rules for Variables..................................................................................................................................5
String Variables in PHP.......................................................................................................................................5
The Concatenation Operator................................................................................................................................6
The strlen() function..............................................................................................................................................6
The strpos() function.............................................................................................................................................6
PHP OPERATORS.......................................................................................................................................................7
PHP OPERATORS........................................................................................................................................................7
Arithmetic Operators............................................................................................................................................7
Assignment Operators...........................................................................................................................................7
Comparison Operators.........................................................................................................................................7
Logical Operators.................................................................................................................................................8
PHP IF...ELSE STATEMENTS..................................................................................................................................8
CONDITIONAL STATEMENTS.......................................................................................................................................8
The if Statement.....................................................................................................................................................8
The if...else Statement...........................................................................................................................................8
The if...elseif....else Statement...............................................................................................................................9
The PHP Switch Statement...................................................................................................................................9
PHP ARRAYS.............................................................................................................................................................10
WHAT IS AN ARRAY?...............................................................................................................................................10
Numeric Arrays...................................................................................................................................................10
Associative Arrays..............................................................................................................................................10
Multidimensional Arrays....................................................................................................................................11
PHP LOOPS................................................................................................................................................................12
WHAT IS LOOP?........................................................................................................................................................12
The while Loop....................................................................................................................................................12
The do...while Statement.....................................................................................................................................12
The for Loop........................................................................................................................................................13
The foreach Loop................................................................................................................................................14
PHP FUNCTIONS......................................................................................................................................................15
Complete PHP 3 CONTENTS
BUILT-FUNCTIONS....................................................................................................................................................15
Create a Function...............................................................................................................................................15
PHP Functions - Adding parameters..................................................................................................................15
PHP Functions - Return values...........................................................................................................................16
PHP
PHP is a more powerful tool for developing dynamic and interactive Web pages. It is the widely-
used and efficient alternative to the giant products like Microsoft's ASP in the present day
market.
In this PHP Book we will learn about PHP, and the way it executes scripts the server. But before
we proceed with PHP what we need to already know is the basic understanding of the following:
HTML/XHTML
JavaScript
About PHP
PHP stands for Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP supports databases like (MySQL, ODBC, Informix, Solid, PostgreSQL, Generic, Oracle,
Sybase, etc.)
PHP runs on different platforms (Windows, Linux, Unix, etc.)
PHP is compatible with all servers (Apache, IIS, etc.)
PHP file
A PHP file can be created using simple text editor
A PHP file can have text, HTML tags and scripts
A PHP file is returned to the browser as simple HTML
A PHP file has extension of ".php", ".php3", or ".phtml"
About MySQL
MySQL is a database server ideal for small and large applications
MySQL is standard SQL compliant
MySQL possess the ability to compile on a number of platforms
PHP + MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)
PHP Installation
In order to access the server with PHP support, you need to install following on your server:
Apache (or IIS), PHP, MySQL
Complete PHP 4 CONTENTS
Basic syntax
PHP scripting block starts with <?php and ends with ?> which can be placed anywhere in the
document. Servers with shorthand support PHP scripting block can be started with <? and end
with ?>.
<?php
?>
PHP file normally has HTML tags and some PHP scripting code.
Example
<html>
<body>
<?php
echo "I am new to PHP";
?>
</body>
</html>
Code line in PHP ends with a semicolon. Semicolon works as separator and distinguishes one set
of instructions from another. In PHP there are two statements to output text: echo and print.
Important note: The file must be saved with .php extension.
Comments in PHP
We use // to make a single-line comment or /* and */ to make a large comment block.
<html>
<body>
<?php
//Single line comment
/*
Complete PHP 5 CONTENTS
Large
block
comment
*/
?>
</body>
</html>
Variables in PHP
Variable
A variable is used for storing value text string, numbers or array. Variable can be used over and
over again after its declaration in script. In PHP, variables start with a $ sign symbol.
The way of declaring a variable in PHP is:
$var_name = value;
Example
<?php
$txt="Hey, I am new to PHP.";
$x=16;
?>
Variable declaration
A variable does not need to be declared before adding a value to it and also you do not have to
tell PHP which data type the variable is. PHP converts the variable to the correct data type
automatically. A variable is declared automatically when you use it.
Output
Hello World
PHP Operators
PHP Operators
Following operators are used in PHP.
Arithmetic Operators
Operator Description Example Result
+ Addition x=2, x+2 4
- Subtraction x=2, 5-x 3
* Multiplication x=4, x*5 20
/ Division 15/5, 5/2 3
2.5
% Modulus (division remainder) 5%2, 1
10%8,10%2 2
0
++ Increment x=5, x++ x=6
-- Decrement x=5, x-- x=4
Assignment Operators
Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
Complete PHP 8 CONTENTS
%= x%=y x=x%y
Comparison Operators
Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
<> is not equal 5<>8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true
Logical Operators
Operator Description Example
&& and x=6, y=3 , (x < 10 && y > 1) returns true
|| or x=6, y=3 , (x==5 || y==5) returns false
! not x=6, y=3, !(x==y) returns true
Conditional Statements
You can use conditional statements to perform different actions for different decisions.
In PHP we have the following conditional statements:
The if Statement
Use if statement to execute some code if a specified condition is true.
Example
<html>
<body>
<?php
$d=date("D");
if ($d=="Sun") echo "We are off today.";
?>
</body>
</html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
Example
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
<?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>
Complete PHP 10 CONTENTS
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
PHP Arrays
What is an Array?
Array is used to store multiple values in single variable. A variable stores only one value. Array
is a special variable, which can store multiple values in one single variable. 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
Numeric Arrays
A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In this example the index are automatically assigned (starting from 0):
$cars=array("Saab","Volvo","BMW","Toyota");
2. In this example index is manually assigned:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
Complete PHP 11 CONTENTS
$cars[3]="Toyota";
Example
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
Output
Saab and Volvo are Swedish cars.
Associative Arrays
An associative array, each ID key is associated with a value. With associative arrays we can use
the values as keys and assign values to them.
Example
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Example
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
The ID keys can be used in a script:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
Output
Peter is 32 years old.
Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each
element in the sub-array can be an array, and so on.
Example
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
Complete PHP 12 CONTENTS
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
Output
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
Example
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
Output
Is Megan a part of the Griffin family?
PHP Loops
What is Loop?
Instead of same set of code again and again we use looping technique to run a block of code over
and over again. In PHP, we have the following looping statements:
while - loops through a block of code while a specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as a specified
condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
Complete PHP 13 CONTENTS
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
Output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>
Output:
The number is 2
The number is 3
The number is 4
Complete PHP 14 CONTENTS
The number is 5
The number is 6
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
</body>
</html>
Output:
one
two
three
PHP Functions
Built-Functions
Complete PHP 16 CONTENTS
In PHP, there are more than 700 built-in functions. To keep the script from being executed when
the page loads, you can put it into a function. A function is executed when it is called. Function
can be called a function from anywhere within a page.
Create a Function
A function will be executed by a call.
Syntax
function functionName()
{
code to be executed;
}
The function name can start with a letter or underscore (not a number)
Example
<html>
<body>
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
</body>
</html>
Output:
My name is Kai Jim Refsnes
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
</body>
</html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
Example 2
<html>
<body>
<?php
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
</body>
</html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
Complete PHP 18 CONTENTS
</body>
</html>
Output:
1 + 16 = 17