0% found this document useful (0 votes)
42 views33 pages

Chapter 2 p2

This document summarizes different types of decision making and looping statements in PHP including if/else, else if, switch, for, while, do/while, and foreach loops. Specific syntax and examples are provided for each statement type to illustrate their usage. Break and continue statements are also introduced and examples given of how they can be used to control loop execution flow.

Uploaded by

kebe Aman
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)
42 views33 pages

Chapter 2 p2

This document summarizes different types of decision making and looping statements in PHP including if/else, else if, switch, for, while, do/while, and foreach loops. Specific syntax and examples are provided for each statement type to illustrate their usage. Break and continue statements are also introduced and examples given of how they can be used to control loop execution flow.

Uploaded by

kebe Aman
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/ 33

Chapter 2 cont….

1 11/17/2023
Chapter 2 Advanced Internet Programming
Decision making statement
The if, else if ...else and switch statements are used to
take decision based on the different condition.
You can use conditional statements in your code to
make your decisions.
PHP supports following three decision making
statements:
if...else statement - use this statement if you want to
execute a set of code when a condition is true and
another if the condition is not true

2 Chapter 2 Advanced Internet Programming 11/17/2023


Continued
else if statement - is used with the if...else statement
to execute a set of code if one of several condition are
true
Switch statement - is used if you want to select one of
many blocks of code to be executed, use the Switch
statement. The switch statement is used to avoid long
blocks of if…else if…else code.

3 Chapter 2 Advanced Internet Programming 11/17/2023


Continued
The If...Else Statement
If you want to execute some code if a condition is true
and another code if a condition is false, use 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
The following example will output "Have a nice
weekend!" if the current day is Friday, otherwise it will
output "Have a nice day!":
4 Chapter 2 Advanced Internet Programming 11/17/2023
Continued
<html>
<body>
<?php$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else echo "Have a nice day!"; ?>
</body>
</html>
If more than one line should be executed if a condition is
true/false, the lines should be enclosed within curly braces:

5 Chapter 2 Advanced Internet Programming 11/17/2023


Continued
 <html>
 <body>
 <?php
 $d=date("D");
 if ($d=="Fri")
 { echo "Hello!<br />";
 echo "Have a nice weekend!";
 echo "See you on Monday!"; }
 ?>
 </body>
 </html>
6 Chapter 2 Advanced Internet Programming 11/17/2023
The else If Statement
 If you want to execute some code if one of several conditions are
true use the elseif statement
 Syntax
if (condition) code to be executed if condition is true;
 elseif (condition) code to be executed if condition is true;
 else code to be executed if condition is false;
Example
 The following example will output "Have a nice weekend!" if the
current day is Friday, and "Have a nice Sunday!" if the current
day is Sunday.
 Otherwise it will output "Have a nice day!":

7 Chapter 2 Advanced Internet Programming 11/17/2023


Continued
<html><body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
 echo "Have a nice Sunday!";
else echo "Have a nice day!";
?>
</body></html>
8 Chapter 2 Advanced Internet Programming 11/17/2023
The Switch Statement
If you want to select one of many blocks of code to be
executed, use the Switch statement. The switch statement is
used to avoid long blocks of if..elseif..else code.
Syntax
switch (expression)
{
case label1: code to be executed if expression = label1; break;
case label2: code to be executed if expression = label2; break;
default: code to be executed if expression is different from
both label1 and label2;
}

9 Chapter 2 Advanced Internet Programming 11/17/2023


Example
The switch statement works in an unusual way.
First it evaluates given expression then seeks a label to
match the resulting value.
If a matching value is found then the code associated
with the matching label will be executed or if none of
the labels match then statement will execute any
specified default code.

10 Chapter 2 Advanced Internet Programming 11/17/2023


Continued
<html><body>
<?php
$d=date("D");
switch ($d)
{case "Mon": echo "Today is Monday"; break;case
"Tue": echo "Today is Tuesday"; break;case "Wed": echo
"Today is Wednesday"; break;case "Thu": echo "Today is
Thursday"; break;case "Fri": echo "Today is Friday";
break;case "Sat": echo "Today is Saturday"; break;case
"Sun": echo "Today is Sunday"; break;default: echo
"Wonder which day is this ?";}?> </body> </html>

11 Chapter 2 Advanced Internet Programming 11/17/2023


PHP LOOP
Loops in PHP are used to execute the same block of code
a specified number of times.
PHP supports following four loop types.
for - loops through a block of code a specified number of
times.
while - loops through a block of code if and as long as a
specified condition is true.
do...while - loops through a block of code once, and then
repeats the loop as long as a special condition is true.
foreach - loops through a block of code for each element
in an array.
12 Chapter 2 Advanced Internet Programming 11/17/2023
The for loop statement
 The for statement is used when you know how many times
you want to execute a statement or a block of statements.
 Syntax
 for (initialization; condition; increment)
{
 code to be executed;
}
 The initializer is used to set the start value for the counter
of the number of loop iterations.
 A variable may be declared here for this purpose and it is
traditional to name it $i.
13 Chapter 2 Advanced Internet Programming 11/17/2023
Example
 The following example makes five iterations and changes the assigned
value of two variables on each pass of the loop:

<html><body>
 <?php $a = 0; $b = 0;
 for( $i=0; $i<5; $i++ )
{
 $a += 10;
 $b += 5;
}
 echo ("At the end of the loop a=$a and b=$b" );
 ?>
 </body>
 </html>

14 Chapter 2 Advanced Internet Programming 11/17/2023


The while loop statement
The while statement will execute a block of code if and
as long as a test expression is true, If the test expression
is true then the code block will be executed.
After the code has executed the test expression will
again be evaluated and the loop will continue until the
test expression is found to be false.
Syntax
while (condition)
{
 code to be executed;
}
15 Chapter 2 Advanced Internet Programming 11/17/2023
Example
This example decrement a variable value on each
iteration of the loop and the counter increments until it
reaches 10 when the evaluation is false and the loop
ends.
<html><body>
<?php $i = 0; $num = 50;
 while( $i < 10)
{ $num--; $i++; }
echo ("Loop stopped at i = $i and num = $num" ); ?>
</body></html>

16 Chapter 2 Advanced Internet Programming 11/17/2023


The do...while loop statement
 The do...while statement will execute a block of code at least
once - it then will repeat the loop as long as a condition is true
 Syntax
 do
{
 code to be executed;
 }while (condition);
 Example
 The following example will increment the value of i at least
once, and it will continue incrementing the variable i as long
as it has a value of less than 10:

17 Chapter 2 Advanced Internet Programming 11/17/2023


Example continued
<html> <body>
<?php $i = 0; $num = 0;
do{ $i++; }while( $i < 10 );
echo ("Loop stopped at i = $i" );
?>
</body> </html>

18 Chapter 2 Advanced Internet Programming 11/17/2023


The for each loop statement
The foreach statement is used to loop through arrays.
For each pass the value of the current array element is
assigned to $value and the array pointer is moved by
one and in the next pass next element will be
processed.
Syntax
foreach (array as value)
{
code to be executed;
}

19 Chapter 2 Advanced Internet Programming 11/17/2023


Example
Try out following example to list out the values of an
array.
<html> <body>
<?php $array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
 echo "Value is $value <br />"; } ?>
</body></html>

20 Chapter 2 Advanced Internet Programming 11/17/2023


The break statement
The PHP break keyword is used to terminate the
execution of a loop prematurely.
The break statement is situated inside the statement
block.
If gives you full control and whenever you want to exit
from the loop you can come out.
After coming out of a loop immediate statement to the
loop will be executed.

21 Chapter 2 Advanced Internet Programming 11/17/2023


Example
In the following example condition test becomes true
when the counter value reaches 3 and loop terminates.

<html> <body> <?php $i = 0; while( $i < 10) {
 $i++;
 if( $i == 3 )break;
}
echo ("Loop stopped at i = $i" );
?> </body></html>

22 Chapter 2 Advanced Internet Programming 11/17/2023


The continue statement
The PHP continue keyword is used to halt the current
iteration of a loop but it does not terminate the loop.

Just like the break statement the continue statement is


situated inside the statement block containing the code
that the loop executes, preceded by a conditional test.

For the pass encountering continue statement, rest of


the loop code is skipped and next pass starts.

23 Chapter 2 Advanced Internet Programming 11/17/2023


Example
In the following example loop prints the value of array
but for which condition becomes true it just skip the
code and next value is printed.

<html> <body>
<?php $array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{ if( $value == 3 )continue;
 echo "Value is $value <br />"; } ?>
</body></html>

24 Chapter 2 Advanced Internet Programming 11/17/2023


PHP - Arrays
An array is a data structure that stores one or more
similar type of values in a single value.

For example if you want to store 100 numbers then


instead of defining 100 variables its easy to define an
array of 100 length.

There are three different kind of arrays and each array


value is accessed using an ID c which is called array
index.

25 Chapter 2 Advanced Internet Programming 11/17/2023


Continued
Numeric array - An array with a numeric index.
Values are stored and accessed in linear fashion
Associative array - An array with strings as index.
This stores element values in association with key
values rather than in a strict linear index order.
Multidimensional array - An array containing one or
more arrays and values are accessed using multiple
indices

26 Chapter 2 Advanced Internet Programming 11/17/2023


Numeric Array
These arrays can store numbers, strings and any object
but their index will be represented by numbers. By
default array index starts from zero.
Example
Following is the example showing how to create and
access numeric arrays.
Here we have used array () function to create array.
This function is explained in function reference.

27 Chapter 2 Advanced Internet Programming 11/17/2023


Continued
<html><body><?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);foreach( $numbers as
$value ){ echo "Value is $value <br />";}/
* Second method to create array. */
$numbers[0] = "one";$numbers[1] = "two";
$numbers[2] = "three";$numbers[3] = "four";
$numbers[4] = "five"; foreach( $numbers as $value ){
echo "Value is $value <br />";}?></body></html>

28 Chapter 2 Advanced Internet Programming 11/17/2023


Associative Arrays
The associative arrays are very similar to numeric arrays in
term of functionality but they are different in terms of their
index.
Associative array will have their index as string so that you
can establish a strong association between key and values.
To store the salaries of employees in an array, a numerically
indexed array would not be the best choice.

Instead, we could use the employees names as the keys in


our associative array, and the value would be their respective
salary

29 Chapter 2 Advanced Internet Programming 11/17/2023


Multidimensional Arrays
 A multi-dimensional array each element in the main array
can also be an array.
 And each element in the sub-array can be an array, and so
on. Values in the multi-dimensional array are accessed using
multiple indexes.
 Example
 In this example we create a two dimensional array to store
marks of three students in three subjects: This example is an
associative array, you can create numeric array in the same
fashion.

30 Chapter 2 Advanced Internet Programming 11/17/2023


<html><body><?php $marks = array( "mohammad"
=> array ("physics" => 35,"maths" =>
30,"chemistry" => 39),"qadir" => array ( "physics" =>
30,"maths" => 32, "chemistry" => 29 ), "zara"
=> array ( "physics" => 31, "maths" => 22,
"chemistry" => 39 )); /* Accessing multi-dimensional
array values */
echo "Marks for mohammad in physics : " ; echo
$marks['mohammad']['physics'] . "<br />"; echo "Marks
for qadir in maths : "; echo $marks['qadir']['maths'] .
"<br />"; echo "Marks for zara in chemistry : " ; echo
$marks['zara']['chemistry'] . "<br />"; ?></body></html>
31 Chapter 2 Advanced Internet Programming 11/17/2023
Some function in php
The strlen() function is used to find the length of a
string. Let's find the length of our string "Hello
world!":
<?php
echo strlen("Hello world!");
?>
This will produce following result:
12

32 Chapter 2 Advanced Internet Programming 11/17/2023


Using the strops () function
 The strpos() function is used to search for a string or
character within a string.
 If a match is found in the string, this function will return
the position of the first match.
 If no match is found, it will return FALSE.
 Let's see if we can find the string "world" in our string:
 <?php
 echo strpos("Hello world!","world");
 ?>
 This will produce following result:
6

33 Chapter 2 Advanced Internet Programming 11/17/2023

You might also like