0% found this document useful (0 votes)
5 views

php2

The document provides an overview of PHP conditional statements, including if, if...else, if...elseif...else, and switch statements, along with their syntax and examples. It also covers PHP arrays, their usage, and the count() function, as well as different types of loops in PHP such as while, do...while, for, and foreach, with corresponding syntax and examples. Overall, it serves as a basic guide to understanding control structures and data handling in PHP.

Uploaded by

bassel.2123018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

php2

The document provides an overview of PHP conditional statements, including if, if...else, if...elseif...else, and switch statements, along with their syntax and examples. It also covers PHP arrays, their usage, and the count() function, as well as different types of loops in PHP such as while, do...while, for, and foreach, with corresponding syntax and examples. Overall, it serves as a basic guide to understanding control structures and data handling in PHP.

Uploaded by

bassel.2123018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

PHP

Faisal Salaheldeen
STEM Obour
PHP if Statements
• Conditional statements are used to perform different actions
based on different conditions.

Syntax
• if (condition) {
code to be executed if condition is true;
}

• Example
• <?php
$x= 5;
if ($x > “3") {
echo "Have a good day!";
}
?>
PHP - The if...else Statement
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

Example
• <?php
$x= 5;
if ($x > “3") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The if...elseif...else Statement
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this
condition is true;
} else {
code to be executed if all conditions are false;
}
Example
<?php
$t = 30;

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The switch Statement
Syntax
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;
}
• Example

• <?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Arrays
An array stores multiple values in one single variable:

• <?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2]
. ".";
?>

The result will be


I like Volvo, BMW and Toyota.
The count() function is used to return the length (the
number of elements) of an array:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Result is
3
Each items in array have an index which start by zero for
first element.
Echo $cars[1]; the result will be BMW
• Butt we can give a unique index for each element.
Example
• <?php
$foo = array('bar' => 'baz');
echo "Hello {$foo['bar']}!"; // Hello baz!
?>
Example
<?php
$a = [1, 2, 3, 4];
print_r($a);
?>

The above example will output:


Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
PHP Loops
Often when you write code, you want the same block of code to
run over and over again a certain number of times. So, instead
of adding several almost equal code-lines in a script, we can use
loops.

• In PHP, we have the following loop types:


• while - loops through a block of code as long as the specified
condition is true
• do...while - loops through a block of code once, and then
repeats the loop as long as the 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
while Loop
The while loop - Loops through a block of code as long as the specified
condition is true.
• Syntax
• while (condition is true) {
code to be executed;
}

• Example

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

while($x <= 100) {


echo "The number is: $x <br>";
$x+=10;
}
?>
• Result is

• The number is: 0


The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50
The number is: 60
The number is: 70
The number is: 80
The number is: 90
The number is: 100
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.

• Syntax
• do {
code to be executed;
} while (condition is true);
• Example
<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
for Loop
The for loop - Loops through a block of code a specified number
of times.

Syntax
• for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}

Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
foreach Loop
The foreach loop - Loops through a block of code for each
element in an array.
Syntax
• foreach ($array as $value) {
code to be executed;
}

Example
• <?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>

You might also like