PHP Book.
PHP Book.
Echo statement 3
Variables 4
Data Types 4
Comments 6
Constant variables 6
Operators 7
Assignment Operators 9
Comparison Operators 9
Comparison operator 11
If statement 11
If-else statement 12
Elif statement 13
Functions in PHP 15
Functions with parameters 16
Function with returning value 17
Variable functions in PHP 18
Anonymous function 19
Local and Global variables 19
Arrays 22
Associative Array 24
Foreach Loops 25
Page 1 of 43
Multidimensional arrays 27
Array-count & Size of function 29
In_Array () & Array_Search () 31
Array-replace () & Array_replace_recursive () 32
Array POP & PUSH 35
Array Shift & Unshift 37
Array Merge & Array Combine 39
Array slice 42
Page 2 of 43
Chapter 01
PHP
PHP is a server side scripting language that is embedded in HTML. It is used to
manage dynamic content, databases, session tracking, even build entire e-
commerce sites. It is integrated with a number of popular databases, including
MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. Basic
structure of PHP
Echo statement
Page 3 of 43
The following statement will print
This is Basic Code of PHP.
Variables
In PHP Variables starts with “$” sign. We can make Variables by our self
Example:
$a = 34;
$v = 2;
$s = 45;
These are the examples of variables
Following is the Code of using Variables.
<? Php
$a = 34;
Echo $a;
?>
The following code will print the value of “$a” or our desired Variable.
Data Types
Page 4 of 43
2. Float
3. String
4. Boolean
5. Array
In PHP we use var_dump () function to know the type of a variable.
?>
This will give following solution
string(11) "NASIR ABBAS"
int(23)
Page 5 of 43
float(43.43)
bool(true)
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1)
"4" [4]=> string(1) "5"
Comments
Constant variables
Page 6 of 43
Define( “a” ,34);
Echo a;
?>
This will give “34” as an output.
Now if we change the value
<?php
Define (“a”, 34);
Define (“a”, 37);
Echo a;
?>
It will give error. As the constant value cannot be changed.
Operators
<? Php
Echo "<h1>ARITHMATIC OPERATIONS </h1> <br>";
Page 7 of 43
$a= 200;
$b=10;
Echo “a= 200 <br> b=10 <br>";
echo "The Addition(+) of a and b is: ", $a+$b, "<br>";
echo "We can also do that (a+b)*2 or any other operations <br>";
echo "The Subs traction(-) of a and b is: ", $a-$b, "<br>";
echo "The Multiplication(*) of a and b is: ", $a*$b, "<br>";
echo "The Division(/) of a and b is: ", $a/$b, "<br>";
echo "The Remainder(%) of a and b is: ", $a%$b, "<br>";
echo "The Exponential(**) of a is: ", $a**$b, "<br>";
echo "The Increment(++) of a is: ", $a++, "<br>";
echo "The Decrement(--) of b is: ", --$b, "<br>";
?>
This will give following output
a= 200
b=10
The Addition(+) of a and b is: 210
We can also do that (a+b)*2 or any other operations
The Subs traction(-) of a and b is: 190
The Multiplication(*) of a and b is: 2000
The Division(/) of a and b is: 20
The Remainder(%) of a and b is: 0
The Exponential(**) of a is: 1.024E+23
The Increment(++) of a is: 200
The Decrement(--) of b is: 9
Page 8 of 43
Assignment Operators
$a=10;
$b=20;
echo $a;
Comparison Operators
<?php
Page 9 of 43
echo "<h1>Comparison operator</h1> <br>";
$a=20;
$b=10;
echo "a is equal to b and there data type is same is : ", $a===$b,"<br>";
echo "a is not equal to b but data type is same is : ", $a!==$b,"<br>";
?>
Page 10 of 43
The output is following
Comparison operator
a=20
b=10
a is equal to b is :
a is not equal to b is : 1
a is equal to b and there data type is same is :
a is not equal to b but data type is same is : 1
a is greater then b is : 1
a is less then b is :
a is not equal to b is : 1
a is less then equal to b is :
a is greater then equal to b is : 1
a is spaceship to b is : 1
If statement
Page 11 of 43
$a=2;
$b=4;
$c=$a+$b;
$d=$a*$b;
echo $d ,"<br>";
echo $c ,"<br>";
if ($a<$b){
echo "A is smaller then b<br>";
}
if ($a*$b == $c){
echo "a and b is equal to c";
?>
The following function will give output as
8
6
A is smaller then b
If-else statement
Page 12 of 43
We use else statement after “If” statement to see if our condition is not run so
what to do next.
Example:
<?php
$age=10;
if($age>18){
echo "you are adult";
}
else{
echo "you are a child";}
?>
The following code will print
You are a child.
Elif statement
We use “elif” statement after if and else statement to see if our both if and else
conditions are not run what to do next.
Example:
<?php
echo "<h1>ELIF STATEMENT</h1><BR>";
Page 13 of 43
$ruppe = 10;
?>
The following code will give following output
ELIF STATEMENT
Page 14 of 43
Functions in PHP
A function is like set of data. We store data in that function and whenever we
want to echo that data we call that function.
We can use functions is PHP as following
Function xyz(it is name of our function) (){
Our desired data
}
Example:
<?php
function nasir(){
$a= 10;
if ($a == 20) {
echo "a is equal to 20";
}
else {
echo " a is not equal to 20 ";
}
}
echo nasir();
?>
Page 15 of 43
The following code will give following output
a is not equal to 20
function sum($a,$b){
echo $a+$b;
}
sum(2,4)
?>
The following code will give “6” as output.
We can assign different values to the sum() function.
Another Example:
<?php
function sum($math,$eng,$bio){
return $sum;
$total=sum(10,20,30);
Page 17 of 43
echo $total;
?>
function dup(){
$fun1 = "dup";
$fun1();
?>
In this example first we created a function called “dup”
Page 18 of 43
Then after creating function we assigned it a variable called “$fun1”
Anonymous function
$anonymous = function () {
Page 19 of 43
<?php
function local(){
$a=20;
local();
echo "outside value of : ",$a;
?>
The following function will give an error as mentioned above that local variables
will not work inside the functions.
Global variables: For global variables we call variables inside the functions by
writing a special statement “Global” with variables.
Example:
<?php
$s=10;
function local(){
global $s;
echo "This is local variable s : " , $s, "<br>" ;
}
Page 20 of 43
local();
echo "Outside value of Global variable is : ", $s, "<br>";
?>
In this code we call special word “Global” to the variables and Hence Now it will
work everywhere inside and outside of the function too because it is a global
variable Now.
The output of the following code will be following:
This is local variable s : 10
Outside value of Global variable is : 10
Page 21 of 43
Chapter 02
Arrays
Method 1:
<?php
$a =array ( "red", "white" , 20 , 10.6, true);
echo $a[2];
?>
This is first method of calling an array and the output of the function will be “20”.
Method 2:
By “print_r” function
<?php
$a = ["red", "white" , 20 , 10.6, true];
Page 22 of 43
print_r($a)
?>
The output of the following array will be
Array ( [0] => red [1] => white [2] => 20 [3] => 10.6 [4] => 1 )
And by using <pre></pre> the output will be
Using Pre
echo "<pre>";
print_r($a);
echo "</pre>";
Array ( [0] => red [1] => white [2] => 20 [3] => 10.6 [4] => 1 )
Method 03:
Page 23 of 43
"Naseer 20",
"NOman 20",
"Shahbaz 20",
);
// echo "<pre>";
// print_r($nameage);
// echo "</pre>";
for ($i=1; $i < 6 ; $i++) {
echo $a[$i] , "<br>";
}
?>
The output of the following will be
Nasir junior 20
Abbas 20
Naseer 20
NOman 20
Shahbaz 20
Associative Array
$task = array (
Page 24 of 43
'Nasir' => 20,
'Musk' => 2044,
'Gates' => 270,
'Interpreter' => 50,
);
Foreach Loops
We use Foreach loops to print arrays in simple forms.
The basic syntax of foreach loop is following
Page 25 of 43
foreach ($variable as $key => $value) {
# code...
}
The following syntax is for associative arrays
foreach ($variable as $value) {
# code...
}
The following syntax is for every arrays.
Example:
<?php
$name = [
'Nasir' => 20 ,
'Abbas' => 20 ,
'Shahbaz' => 20 ,
'Malik' => 20 ,
];
foreach ($name as $key => $value) {
echo "$key = $value", "<br>";
}
?>
Page 26 of 43
The output of the following code is
Nasir = 20
Abbas = 20
Shahbaz = 20
Malik = 20
Multidimensional arrays
These are like nested arrays. We can also use it with for loop or for each loop
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multideminsional arrays</title>
</head>
<body>
<?php
$data = [
Page 27 of 43
[1, "Nasir ", "Head Teacher", " 12 years", 60000],
[2, "Haider ", "second Teacher", " 11 years", 30000],
[3, "Asad ", "Third Teacher", " 9 years", 20000],
[4, "Nadir ", "Fourth Teacher", " 5 years", 10000],
[5, "Basit ", "Naib Qasid", " 2 years", 6000],
];
// for ($dat=0; $dat <5 ; $dat++) {
// for ($col=0; $col <5 ; $col++) {
// echo $data[$dat][$col] . "-" ;
// }
// echo "<br>";
// }
echo "<table border=2px cellspacing= '0px' >";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Job Position</th>";
echo "<th>Experience </th>";
echo "<th>Salary </th>";
foreach ($data as $col1) {
echo "<tr>";
Page 28 of 43
foreach ($col1 as $col2) {
echo "<td>" . $col2 . " ";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
We can know the Size of Array by printing the parameter “Size of ()” or “Count ()”
Example:
Page 29 of 43
<?php
$card = "Result Card of ";
$name = "Nasir Abbas";
$result = array(
'PHYSICS' => 30,
'CHEMISTRY' => 40,
'MATH' => 70,
'BIO' => 60,
);
echo $card. $name , "<br>" ,count($result), "<br>";
echo sizeof($result);
?>
The following will give output as
Result Card of Nasir Abbas
4
4
But if we use Multidimensional Arrays then we use a mauled with count or sizeof
function
Eg: sizeof($arrayname , 1) or count($arrayname , 1) by default the value is 0 but
when we make it 1 then it counts all the values in multidimensional arrays.
Example:
<?php
Page 30 of 43
echo "Multidementional Array <br>";
$repee = [
'num1' => array(10,20 ,30),
'num2' => array(40,50,60)
];
echo count($repee,1);
?>
Page 31 of 43
This will give 1 as output because the name “Nasir” is available in the array.
Example2 for array_search
<?php
$name = array( "Nasir" , "Abbas" ,"Khadim", "Hussain");
We can replace array by another array but it will not replace in the existing two
arrays. If we want to replace array then we will make another array
Example:
<?php
Page 32 of 43
"d" => "Banana3"
];
echo "<Pre>";
print_r($subMarks);
echo "</Pre>";
?>
The following will give output as
Array
(
[0] => 23
[1] => 34
[2] => 54
[3] => 45
[4] => 34
[a] => Banana
[b] => Banana1
[c] => Banana2
[d] => Banana3
)
Page 33 of 43
'MANGO' => "NOT COLOR"
),
"b" => $multidem2 = array(
'Bio' => 50 ,
'math' => 80 ,
'chem' => 40
)
];
$array2 = [
"a" => $multidem3 = array('FRUIT1' => "BANANA1" ,
'COLOR1' => "RED1",
'MANGO1' => "NOT COLOR1"
),
"b" => $multidem4 = array(
'Bio1' => 150 ,
'math1' => 180 ,
'chem1' => 140
)
];
$new = array_replace_recursive($array1 ,$array2);
echo "<Pre>";
print_r($new);
Page 34 of 43
echo "</Pre>";
Output as following
Array
(
[a] => Array
(
[FRUIT] => BANANA
[COLOR] => RED
[MANGO] => NOT COLOR
[FRUIT1] => BANANA1
[COLOR1] => RED1
[MANGO1] => NOT COLOR1
)
POP
We can delete any value from an array by following
<?php
$result = [
$subject = array ('physics ', 'math' , 'bio'),
$number = array (50 , 53 , 23 )
Page 35 of 43
];
array_pop($subject);
echo "<pre>";
print_r($subject);
echo "</pre>";
?>
The output of the code is following
Array
(
[0] => physics
[1] => math
)
The last value of the array deleted.
PUSH
We can add something in our array by following
We can also push multiple values at the same time
<?php
$result = [
$subject = array ('physics ', 'math' , 'bio'),
$number = array (50 , 53 , 23 )
];
array_push($number,30);
echo "<pre>";
Page 36 of 43
print_r($number);
echo "</pre> <br>";
?>
The output of the following code is
Array (
[0] => 50
[1] => 53
[2] => 23
[3] => 30
We add the new value “30” hence we pushed the value in array.
Shift
It will remove the first value of an array.
<?php
Page 37 of 43
?>
Output
Array
(
[0] => math
[1] => bio
)
Unshift
It will add new value on the first index of an array.
We can add many values at the same time.
<?php
?>
Output
Array ( [0] => nasir [1] => math [2] => bio )
Page 38 of 43
Array Merge & Array Combine
$ralimli = array_merge($cake,$fruit);
echo "<pre>";
print_r($ralimli);
echo "</pre>";
?>
Output
Array
(
[0] => Cup cake
[1] => Banana cake
[2] => Mango cake
[3] => Orange cake
[4] => Mango
[5] => Lemon
[6] => Orange
)
Page 39 of 43
Example:
<?php
$school = [
"A" => $class1 = array ('Haider ' , 'Nadir' , 'Abbas'),
"B" => $class2 = array ('Shuboo ' , 'Nasir' , 'Adnan'),
];
$highsh = [
"C" => $class7 = array ('Janu ' , 'Babu' , 'Jani'),
"D" => $class8 = array ('Ahmad ' , 'Lucky' , 'Adoo'),
];
$ultrahigh = array_merge_recursive($school,$highsh);
echo "<pre>";
print_r($ultrahigh);
echo "</pre>";
?>
Output
Array
(
[A] => Array
(
[0] => Haider
[1] => Nadir
[2] => Abbas
)
Page 40 of 43
[0] => Shuboo
[1] => Nasir
[2] => Adnan
)
Array combine
It can merge only index array.
Example:
<?php
$cake = ["Cup cake" , "Banana cake " , "Mango cake " , "Orange cake"];
$cakepoint = ['60' , '50 ' , '70' , '46'];
$newarray = array_combine($cake,$cakepoint);
echo "<pre>";
print_r($newarray);
echo "</pre>";
?>
Output
Array
(
[Cup cake] => 60
Page 41 of 43
[Banana cake ] => 50
[Mango cake ] => 70
[Orange cake] => 46
)
Array slice
$cake = ["Cup cake" , "Banana cake " , "Mango cake " , "Orange cake"];
?>
Output
Array
(
[1] => Banana cake
[2] => Mango cake
[3] => Orange cake
)
Page 42 of 43
Hence by writing “ True ” we have the same index.
Page 43 of 43