1 of 15

Web Technologies

(COURSE CODE: B18CS3102)

Control Statements

Smt.M.Jeevana Sujitha

Assistant Professor

Department of Computer Science and Engineering

SRKR Engineering College, Bhimavaram, A.P. - 534204

2 of 15

OBJECTIVES

The Objective of this lecture is

  • To learn about control statements in php.

3 of 15

Control Statements

Conditional Statements:

  • Conditional statements are perform different actions

used to based on

different conditions.

  • if statement
  • if-else statement
  • else if ladder
  • switch

4 of 15

Control Statements

If statement: The if statement executes some code if one

condition is true.

Syntax:

if(condition)

{

Code to be executed if condition is true

}

Example:

<?php

$age=19; if($age==18||$age>18) echo “eligible for voting”;

?>

5 of 15

Control Statements

if-else statement: The if-else statement executes some code if a condition is true and another code if that condition is false.

Syntax:

if(condition)

{

Code to be executed if condition is true

}

else

{

Code to be executed if condition is false

}

Example:

<?php

$age=19; if($age==18||$age>18) echo “eligible for voting”; else

echo “Not eligible for voting”;

?>

6 of 15

Control Statements

else if ladder: The else if ladder statement executes different

codes for more than two conditions.

Syntax:

if(condition)

{

code to be executed if this condition is true;

}

else if(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;

}

7 of 15

Control Statements

Switch Statement: The switch statement is used to perform different actions based on different conditions.

  • We can use the switch statement to select one of many blocks of code to be

executed.

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;

}

8 of 15

Control Statements

Looping Statements: If we want to do the same task for required number of times then we go for looping concept.

  • Loops are used to execute the same block

of code again and again, as long as a certain condition is true

  • In PHP, we have the following loop types:
  • while
  • do-while
  • for
  • foreach

9 of 15

Control Statements

while loop: The while loop executes a block of code as long as the specified condition is true.

Syntax: while(condition)

{

Code to be executed;

}

Example:

<?php

$x = 1;

while($x <= 10)

{

echo “srinu”;

$x++;

}

?>

10 of 15

Control Statements

do-while loop: The do-while loop always executes a block of code once, it

will then check the condition and repeat the while loop as long as the

specified condition is true.

Syntax:

do

{

code to be executed;

} while (condition is true);

Example:

<?php

$x = 1;

do

{

echo “srinu";

$x++;

}while($x<=10);

?>

11 of 15

Control Statements

for loop: The for loop is executed a block of code a specified

number of times.

  • The for loop is used when you know in advance how many times the script should run.

Syntax:

for (initialization; condition; increment/decrement)

{

code to be executed for each iteration;

}

Example:

<?php

for ($x = 1; $x <= 10; $x++)

{

echo “srinu";

}

?>

12 of 15

Control Statements

foreach loop: The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Syntax:

foreach ($array as $value)

{

code to be executed;

}

  • 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.

Example:

<?php

$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {

echo "$value <br>";

}

?>

13 of 15

Control Statements

Jumping Statements:

Break: The break statement can be used to jump out of a

loop or switch statement.

Example:

<?php

for ($x = 1; $x <= 10; $x++)

{

if ($x == 5)

{

break;

}

echo "The number is: $x <br>";

}

?>

14 of 15

Control Statements

Jumping Statements:

Continue: The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

Example:

<?php

for ($x = 1; $x <= 10; $x++)

{

if ($x == 5)

{

continue;

}

echo "The number is: $x <br>";

}

?>

15 of 15

THANK YOU