Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Table of Contents

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

The basic code of PHP is following:


<? Php
Echo “This is Basic Code of PHP.”;
?>

The following statement will print


This is Basic Code of PHP.

Echo statement

We use “echo” Statement in PHP to print something.


For Example:
<? Php
Echo “This is Basic Code of PHP.”;
?>

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

There are six Data Types in PHP.


These are following.
1. Integer

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.

Following is the code to know the Data type of variable.


<? Php
$a = “NASIR ABBAS”;
$b = 23;
$c = 43.43;
$d = true;
$e = array (“1”, “3”, “4”, “5”);
Echo var_dump ($a). “<br>”;
Echo var_dump($b) . “<br>”;
Echo var_dump($c) . “<br>”;
Echo var_dump($d) . “<br>”;
Echo var_dump($e) . “<br>”;

?>
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

There are two types of comments in PHP.

1. Single line comments


These are starts with “#”.
2. Multiple line comments or Double line comments
These are starts with “//”.

Constant variables

A constant variable is a variable whose value cannot be changes after define.


We write constant variable by following method
Define (variable,value,true or false);
True or false will show us the case sensitive
If we define a number called num and case sensitive is true.
Then when we echo that num we write “Num” instead “num” then it will run
correctly.
Example:
<?php

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

Like any other language there are same operators in PHP


+, -, *, /, %, **, ++, -- these are operators.
Many others are assignment (=) operators
>=, <=,! = these are the operators.
Following is the example of 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

We can use Assignment Operator as following


<?php
echo"<h1>Assignment operators</h1> <br>";

$a=10;
$b=20;

// the use of assignment operator is

$a+=$b; // $a= $a+$b; #we can also write it by $a+=$b;


$a-=$b; #this can give a-b
$a*=$b; #this can give a*b
// we can do all arithmetic operations by using assignment operators

echo $a;

Comparison Operators

We can use comparison operators as following


Following are the comparison operators.
Example:

<?php

Page 9 of 43
echo "<h1>Comparison operator</h1> <br>";

$a=20;

$b=10;

echo "a=20 <br> b=10 <br>";

echo "a is equal to b is : ", $a==$b, "<br>";

echo "a is not equal to b is : " ,$a!=$b ,"<br>";

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>";

echo "a is greater then b is : ", $a>$b,"<br>";

echo "a is less then b is : " ,$a<$b,"<br>";

echo "a is not equal to b is : ", $a<>$b,"<br>";

echo "a is less then equal to b is : ", $a<=$b,"<br>";

echo "a is greater then equal to b is : ", $a>=$b,"<br>";

echo "a is spaceship to b 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

In PHP we use If statement by following


if (condition) {
# code...
}
First we take variable then we print our conditions.
Example:
<?php

echo " <h1>If_Statement</h1><br>";

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;

if ($ruppe == 60 and $ruppe <= 120 ) {


echo " U will get less then 2 kg sugar.";
}
elseif ($ruppe == 60 and $ruppe <= 180 ) {
echo " U will get less then 3 kg sugar.";
}
elseif ($ruppe >= 180 and $ruppe <= 300 ) {
echo " U will get less then 5 kg sugar.";
}
elseif ($ruppe >= 400) {
echo " U will get a lot of sugar.";
} else {
echo " you will get nothing.";

?>
The following code will give following output
ELIF STATEMENT

you will get nothing.

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

Functions with parameters

We use different parameters in different functions.


We can assign values to the functions which are called parameters.
Following is the code of parameters in functions.
Example:
<?php

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 Name($Fname = "FIRST NAME", $Lname ="LAST NAME"){


echo "$Fname $Lname"."<br>";
Page 16 of 43
}
Name("Nasir","abbas")
?>
This will give “NASIR ABBAS” as output.

Function with returning value

We can use return function in PHP


Return will use when we call the function in different places and use that function
in different ways.
Example:
<?php

function sum($math,$eng,$bio){

$sum = $math + $eng + $bio;

return $sum;

$total=sum(10,20,30);

Page 17 of 43
echo $total;

?>

The following code will give the total sum of subjects.


Which is 60.

Variable functions in PHP

Like variables in PHP we can assign a variable to the function too.


Example:
<?php

function dup(){

echo "Hello Piyary ";

$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”

The output of the following code is “Hello Piyary”.

Anonymous function

In anonymous function we create a function but we cannot give it a name.


Example:
<?php

$anonymous = function () {

echo "Hello Anonymous";


};
$anonymous ();
?>

The following function is an anonymous function as we cannot give it a name.


The output of the following code is “Hello Anonymous”.

Local and Global variables


Local variables are those variables which are inside the functions. These variables
will work only inside the functions these will give error while calling outside the
function.
Example:

Page 19 of 43
<?php

function local(){
$a=20;

echo "this is local variable a " , $a;


}

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

In an array we can store Strings, integers, Boolean etc.


Array works on the method of index and index starts from 0.
There are three different methods of writhing an array.
They are following

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:

The third method of calling an array is by using loops.


Example:
<?php
$a = Array
(
"Nasir 20",
"Nasir junior 20",
"Abbas 20",

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

In Associative arrays we use “keys” and values in the arrays.


Example:
<?php

$task = array (

Page 24 of 43
'Nasir' => 20,
'Musk' => 2044,
'Gates' => 270,
'Interpreter' => 50,
);

echo "<pre>" , "<li>";


echo "The output of the Key ['Musk'] is : ", $task['Musk'] , "<br>","<li>";
echo "The output of the Key ['Gates'] is : ", $task['Gates'] , "<br>","<li>";
echo "The output of the Key ['Interpreter'] is : ", $task['Interpreter'] ,
"<br>","<li>";
echo "The output of the Key ['Nasir'] is : ", $task['Nasir'] , "<br>";
echo "</pre>";
?>In the following code
“Nasir”, “Musk”, etc. are the keys and in the integer form these are the values of
these keys.
We can echo a key to call or print its value.
The output is following
 The output of the Key ['Musk'] is: 2044
 The output of the Key ['Gates'] is: 270
 The output of the Key ['Interpreter'] is: 50
 The output of the Key ['Nasir'] is: 20

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>

The following example is for multidimensional arrays


Output of the Code is following

ID Name Job Position Experience Salary


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

Array-count & Size of function

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);
?>

And the output of the following code is:


Multidimensional Array
8

8 is the total values in Arrays

In_Array () & Array_Search ()

In_array () and array_search () both are used to search something in arrays.


If we search something in in_array() then it will give output as 0 or 1 . 0 means
that the value is available in following array and vice versa. On the other hand if
we want to search something in Array_search () then it will give the index number
of that value.
Example:
<?php
$name = array( "Nasir" , "Abbas" ,"Khadim", "Hussain");

echo in_array("Nasir" ,$name);


?>

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");

echo array_search("Abbas" ,$name);


?>
This will also give 1 as output as the word “Abbas” is at 1 index.

Array-replace () & Array_replace_recursive ()

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

$subjects = ["Maths " , "Physics" , "Bio"];


$Marks = [ 23, 34, 54, 45, 34];
// if we use associative array then it will replace its index values
// also it is method of replacing three arrays
$associativearray = [
"a" => "Banana",
"b" => "Banana1",
"c" => "Banana2",

Page 32 of 43
"d" => "Banana3"
];

$subMarks = array_replace($subjects , $Marks , $associativearray);

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
)

If we use multidimensional array the we will use array_replace_recursive ()


function
Example:
<?php
$array1 = [
"a" => $multidem1 = array('FRUIT' => "BANANA" ,
'COLOR' => "RED",

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
)

[b] => Array


(
[Bio] => 50
[math] => 80
[chem] => 40
[Bio1] => 150
[math1] => 180
[chem1] => 140
)

Array POP & PUSH

 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.

Array Shift & Unshift

 Shift
It will remove the first value of an array.
<?php

$subject = array ('physics ', 'math' , 'bio');


array_shift($subject);
echo "<pre>";
print_r($subject);
echo "</pre> <br>";

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

$subject = array ('physics ', 'math' , 'bio');


array_shift($subject);
array_unshift($subject , 'nasir');
echo "<pre>";
print_r($subject);
echo "</pre> <br>";

?>

Output
Array ( [0] => nasir [1] => math [2] => bio )

Page 38 of 43
Array Merge & Array Combine

There are three methods for array Merge & combine


 Array merge
It can merge index or associative array.
Example:
<?php
$cake = ["Cup cake" , "Banana cake " , "Mango cake " , "Orange cake"];
$fruit = ['Mango ' , 'Lemon ' , 'Orange'];

$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
)

 Array Merge Recursive


It can merge multidimensional associative array.

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
)

[B] => Array


(

Page 40 of 43
[0] => Shuboo
[1] => Nasir
[2] => Adnan
)

[C] => Array


(
[0] => Janu
[1] => Babu
[2] => Jani
)

[D] => Array


(
[0] => Ahmad
[1] => Lucky
[2] => Adoo
)

 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

It is a function used to cut the array


Its method is following
Array_slice ( array name , start , length ) if we add new parameter “True / False”
near with length then it will give the same index number of the values which
already have their index number. The values we give to the array that index
values will safe other will be removed.
Example: <?php

$cake = ["Cup cake" , "Banana cake " , "Mango cake " , "Orange cake"];

$new = array_slice($cake , 1 ,3 , true );


echo "<pre>";
print_r($new);
echo "</pre>";

?>
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

You might also like