0% found this document useful (0 votes)
27 views6 pages

Chapter 1.3-1.4

php

Uploaded by

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

Chapter 1.3-1.4

php

Uploaded by

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

Chapter 1

Expressions and control statements in PHP

1.3 Decision making Control statements- if , if-else , nested if , switch , break


and continue statement
Decision making (Flow control) statements are used to control flow of execution of programs.

if control statement : It is used to check whether the condition/expression is true or not. If the
expression / condition evaluates to true then block code associated with the if statement is executed.
Syntax : if (expression / condition)
{
Block of code;
}
Example :
<?php
$a=10;
if ($a<20)
echo "variable a is less than 20";
?>
Output : variable a is less than 20

if-else control statement : It is used to check whether the condition/expression is true or false. If
the expression / condition evaluates to true then true block code associated with the if statement is executed
otherwise if it evaluates to false then false block of code associated with else is executed.
Syntax : if (expression/condition)
{
True code block;
}
else
{
False code block;
}
Example :
<?php
$a=30;
if ($a<20)
echo "variable a is less than 20";
else
echo "variable a is greater than 20";
?>
Output :variable a is greater than 20

OR
PHP provides another syntax for blocks in tests and loops. Instead of enclosing the block of statements in
curly braces, place a colon(:) at the end of if statement and use a keyword endif followed by semicolon at
the end of block.
Example:
<?php
$a=30;
if ($a<20):
echo "variable a is less than 20";
else:
echo "variable a is greater than 20";
endif;
?>
Output :variable a is greater than 20

Nested if statement : PHP provides a facility to embed one if statement inside another if statement.
Syntax:
if (expression/condition)
{
if (expression/condition)
{
Statements;
}
}
Example:
$a=55;
if ($a>20)
{
if ($a>50)
{
echo "Variable a is greater than 20 and 50";
}
}
Output: Variable a is greater than 20 and 50

Nested if-else statement : PHP provides a facility to embed one if-else statement inside another if-
else statement.
Syntax:
if(expression/condition)
{
if(expression/condition)
{
Statements;
}
else
{
Statements;
}
}
else
{
Statements;
}
Example :
<?php
$a=55;
if ($a>20)
{
if ($a<50)
{
echo "variable a=$a is greater than 20 and less than 50";
}
else
{
echo "variable a=$a is greater than 20 and greater than 50";
}
}
else
{
echo "variable a=$a is less than 20";
}
?>
Output : variable a=55 is greater than 20 and greater than 50

Ternary conditional operator (? :) : It is used to shorten simple true/false tests. An expression


/ condition,true statement and false statement are placed in a single line. If the expression / condition
evaluates to true then true statement executes otherwise if it is false then false statement executes.
Syntax :
(expression / condition) ? true_statement : false_statement;
Example :
<?php
$a=55;
echo ($a<100)? "a is less than 100" : "a is greater than 100";
?>

break statement : It is used to jump out of a loop. When break statement executes , flow of control
transfers to the statement out of loop/block of code.
Syntax : break;
Example :
<?php
$a=1;
while ($a<10)
{
if ($a==5)
{
break;
}
echo "Iteration $a <br>";
$a++;
}
?>
Output :
Iteration 1
Iteration 2
Iteration 3
Iteration 4
continue statement : The continue statement continues loop execution by skipping some statements
in a iteration.
Syntax : continue;
Example :
<?php
for ($a=1;$a<8;$a++)
{
if ($a==5)
{
continue;
}
echo "Iteration $a";
}
?>
Output : In the output below, when a = 5 loop ignore echo statement and flow of control passes to for
statement. So Iteration 5 will not be displayed in the output.
Iteration 1Iteration 2Iteration 3Iteration 4Iteration 6Iteration 7

switch statement : It is used to perform different actions based on different conditions. This
statement contains a expression value that is matched with each case value inside switch statement. Block of
code associated with matching case value executes and break statement takes the control out of switch
statement. Only one block of code is selected from multiple blocks, depending on case value and expression
value.
Syntax:
switch (expression/ value)
{
case label1:
Block code to be executed if value=label1;
Break;
case label2:
Block code to be executed if value=label2;
Break;
Default:
Block code to be executed if n is not equal to any case label;
}

Example:
<?php
$color="yellow";
switch ($color)
{
case "red":
echo "Color is red";
break;
case "yellow":
echo "Color is yellow";
break;
default:
echo "No color is selected";
}
?>
Output : Color is yellow
1.4 Loop control structure - while , do-while , for , foreach
Loop control structures are used to repeat same set of statements multiple times till a condition becomes
false.

while loop : If the expression/condition specified with while evaluates to true then the statements inside
loop executes and control is transferred back to expression/condition. Again expression/condition is
evaluated and if true statements inside loop executes. The process of evaluation and execution continues till
expression/condition results as true.
Syntax: while(expression / condition)
{
Statements;
}
Example:
<?php
$a=1;
while($a<=5)
{
echo " Iteration $a";
$a++;
}
?>
Output : Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5

OR
Syntax :
while (expression/condition):
Statements;
endwhile;

Example:
<?php
$a=1;
while($a<=5):
echo " Iteration $a";
$a++;
endwhile;
?>
Output : Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5

do-while loop : In this loop, all the statements inside the loop executes for the first time without
checking any condition. The keyword ‘do’ passes the flow of control inside loop. After executing loop for
first time, expression / condition is evaluated. If it evaluates to true then all statements inside loop again
executes and if it evaluates to false then loop exits and flow of control passes to statement placed outside the
loop. The process of execution and evaluation continues till expression / condition evaluates to true.

Syntax:
do
{
Statemetns;
}while(expression / condition);
Example:
<?php
$a=1;
do
{
print("Iteration 1");
$a++;
}while($a<=0);
?>
Output: Iteration 1 // do statement enters loop and executes all statements inside it and after first iteration
condition is checked. As the condition evaluates to false , loop will not execute again.

for loop : It is used to execute same set of statements multiple times. In for loop variable initialization,
condition and increment / decrement is placed in a single statement. Before starting first iteration a variable
is initialized to specified value. Then condition is checked. If condition is true then statements inside the
loop executes and variable is incremented or decremented. Control then passes to condition. IF condition is
false then control passes to the statement placed outside the loop. The process of condition checking, loop
statement execution and increment /decrement continues till condition is true.

Syntax : for(start/initialization;condition;increment/decrement)
{
Statements;
}
OR
for(start/initialization;condition;increment/decrement):
Statements;
Example :
<?php
for ($a=1;$a<=5;$a++)
{
echo("Iteration $a");
}
?>
Output : Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5

foreach statement : This loop works only with arrays and is used to traverse through values in an
array. For every loop iteration, the value of the current array element is assigned to $value and the array
pointer is moved by one, until it reaches the last array element.

Syntax : foreach ($array_variable_name as #variable_name)


{
Statements;
}
Example :
<?php
$arr=array("Apple","Banana","Orange");
foreach($arr as $fruit)
{
echo("$fruit");
}
?>
Output:
AppleBananaOrange

You might also like