0% found this document useful (0 votes)
2 views41 pages

Intro To PHP Lec-2

The document provides an overview of operators in PHP, categorizing them into groups such as arithmetic, assignment, logical, and comparison operators. It also covers conditional statements, various looping structures (for, while, do-while, foreach), and the concept of arrays, including associative arrays and sorting methods. The document includes examples to illustrate the usage of these concepts in PHP programming.

Uploaded by

jameel717772
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views41 pages

Intro To PHP Lec-2

The document provides an overview of operators in PHP, categorizing them into groups such as arithmetic, assignment, logical, and comparison operators. It also covers conditional statements, various looping structures (for, while, do-while, foreach), and the concept of arrays, including associative arrays and sorting methods. The document includes examples to illustrate the usage of these concepts in PHP programming.

Uploaded by

jameel717772
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Web Design & Development

PHP

1
Operators in PHP

• Operators are used to perform operations on variables and


values.
• PHP divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Increment/Decrement operators
• Logical operators
• Comparison operators
• Equality Operators
• Array operators

2
1. Operators in PHP

3
1. Operators in PHP…
String Operators:

4
1. Operators in PHP…
First Variable Second Variable

Concatenation

Using .=
5
1. Operators in PHP…

6
1. Operators in PHP…

Adds $b in $a

Concatenates $b with $a

7
1. Operators in PHP…

8
1. Operators in PHP…
• Increment/decrement Operators:
• The PHP increment operators are used to
increment a variable's value.
• The PHP decrement operators are used to
decrement a variable's value.
– ++ , --
• $b=$a++
• $b=++$a

9
1. Operators in PHP…
• Increment/decrement Operators:

10
1. Operators in PHP…

Variable Declared

Incremented Before Display

Incremented After Display

Displaying Incremented Value

11
1. Operators in PHP…

12
1. Operators in PHP…
• Logical Operators:
– The PHP logical operators are used to combine
conditional statements.
– AND, OR, NOT,
– &&, ||, !

13
1. Operators in PHP…

Comparison Operators:
– The PHP comparison operators are used to
compare two values (number or string):
– >, <, <=, >=

14
1. Operators in PHP…
• Comparison Operators:

15
1. Operators in PHP…
Integer Value
String Value

Compares Only Values

Strict Comparison, Data Types Should Also Match

16
1. Operators in PHP…

17
Conditional Statements…
• switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
} 18
Looping Statements

• For Loop
• While Loop
• Do-While Loop
• ForEach Loop

19
3. Looping Statements
for loop
• The for loop is used when you know in advance how
many times the script should run.
Syntax:
for (init counter; test counter; increment counter) {
code to be executed;
}
………………………………………………………………………….
for($a=0; $a<10; $a++)
{
//statements
}
20
Looping Statements
• <!DOCTYPE html>
<html>
<body>

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

</body>
</html>
21
Looping Statements
while loop
• The while loop executes a block of code as long as
the specified condition is true.

while(condition is true)
{
//Statements
//Increment/decrement
}

22
Looping Statements
while loop Example:
<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>

23
Looping Statements…
• do-while loop
• The do...while loop will always execute the block of
code once, it will then check the condition, and
repeat the loop while the specified condition is true.
do
{
//Statements
//Increment/decrement
}
While(condition is true);

24
Looping Statements…
do-while loop Example:
<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

25
Looping Statements…
foreach loop
• The foreach loop works only on arrays, and is used
to loop through each key/value pair in an array
– is used to read an entire array

foreach ($array as $value)


{
code to be executed;
}

26
Looping Statements…
foreach loop example:
• <?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>

27
Arrays in PHP
• An array stores multiple values in one single variable
• An array is a special variable, which can hold more
than one value at a time.
• An array is traditionally defined as a group of items
that share certain characteristics
• Each item consists of two components:
– Key
– Value
• PHP doesn’t require that you assign a size to an array
at creation time
28
Arrays in PHP…

Declaring an array:
– $array_name[key] = value;
– $players[0] = “Shahid Khan Afridi”;

• Adding element in an array:


– $players[1] = “Muhammad Amir”;

• Accessing element in an array


– echo $players[0];
29
Arrays in PHP…

Declaring Array
Adding Elements

ForEach Loop

30
Arrays in PHP…

31
Arrays in PHP…
Associative arrays: Arrays with named keys
– $array_name[‘element-name’] = value;
– $players[‘shahid’] = “Shahid Khan Afridi”;

Adding element in an array:


– $players[‘amir’] = “Muhammad Amir”;

Accessing element in an array:


– echo $players[‘shahid’];

32
Arrays in PHP…
Associative Array Declared Using array()

Accessing Elements by Name


33
Arrays in PHP…

34
Arrays in PHP…

Associative array:

foreach ($array as $key => $value) {


echo $key. “ ” . $value;
}

35
Arrays in PHP…

Sorting arrays:
– sort()
• Sorts the array in ascending order
– rsort()
• Sorts the array in descending order

36
Arrays in PHP…
Array Declaration

Sorting Array

ForEach Loop for Displaying Arrays

37
Arrays in PHP…

38
Arrays in PHP…

Reverse Sorting

39
Arrays in PHP…

40
THANK YOU

41

You might also like