Intro To PHP Lec-2
Intro To PHP Lec-2
PHP
1
Operators in PHP
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
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
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
26
Looping Statements…
foreach loop example:
• <?php
$colors = array("red", "green", "blue", "yellow");
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”;
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”;
32
Arrays in PHP…
Associative Array Declared Using array()
34
Arrays in PHP…
Associative array:
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
37
Arrays in PHP…
38
Arrays in PHP…
Reverse Sorting
39
Arrays in PHP…
40
THANK YOU
41