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

Control Structures

1) PHP allows conditional statements like if, if/else, if/elseif/else, switch/case to perform different actions based on logical or comparative conditions. 2) The if statement executes code if the condition is true, if/else executes one block if true and another if false. 3) switch/case selects one of many code blocks to execute based on different conditions.

Uploaded by

Kalash Shandilya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Control Structures

1) PHP allows conditional statements like if, if/else, if/elseif/else, switch/case to perform different actions based on logical or comparative conditions. 2) The if statement executes code if the condition is true, if/else executes one block if true and another if false. 3) switch/case selects one of many code blocks to execute based on different conditions.

Uploaded by

Kalash Shandilya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CONTROL

STRUCTURES
Conditional Statements
• PHP also allows you to write code that perform different
actions based on the results of a logical or comparative
test conditions at run time.

• This means, you can create test conditions in the form of


expressions that evaluates to either true or false and
based on these results you can perform certain actions.
Conditional Statements(contd.)
• There are several statements in PHP that you can use to
make decisions:

- The if statement
- The if...else statement
- The if...elseif....else statement
- The switch...case statement
The if Statement
• The if statement is used to execute a block of code only if
the specified condition evaluates to true.

• This is the simplest PHP's conditional statements and can


be written like:

<?php
$d = date("D");
if($d == "Tue"){
echo "It's Tuesday!";
}
?>
The if Statement(contd.)
<?php
$t = date("H");

if ($t < "20") {


echo "Have a good day!";
}
?>
The if Statement(contd.)
<?php
$num=12;
if($num<100){
echo "$num is less than 100";
}
?>

OUTPUT:
12 is less than 100
The if...else Statement
• The if...else statement allows you to execute one block of
code if the specified condition is evaluates to true and
another block of code if it is evaluates to false.

<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} else{
echo "Have a nice day!";
}
?>
The if...else Statement(contd.)
<?php
$t = date("H");

if ($t < "20") {


echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The if...else Statement(contd.)
<?php
$num=13;
if($num%2==0){
echo "$num is even number";
}else{
echo "$num is odd number";
}
?>

OUTPUT:
13 is odd number
The if...elseif...else Statement
• The if...elseif...else a special statement that is used to combine
multiple if...else statements.

<?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!";
}
?>
The if...elseif...else Statement(contd.)
<?php
$t = date("H");

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The if...elseif...else Statement(contd.)
<?php
$marks=69;
if ($marks<33){
echo "fail";
}
else if ($marks>=34 && $marks<50) {
echo "D grade";
}
else if ($marks>=50 && $marks<65) {
echo "C grade"; OUTPUT:
} B grade
else if ($marks>=65 && $marks<80) {
echo "B grade";
}
else if ($marks>=80 && $marks<90) {
echo "A grade";
}
else if ($marks>=90 && $marks<100) {
echo "A+ grade";
}
else {
echo "Invalid input";
}
?>
The Ternary Operator
• The ternary operator provides a shorthand way of writing the if...else
statements.

• The ternary operator is represented by the question mark (?) symbol


and it takes three operands: a condition to check, a result for true, and
a result for false.

<?php
$age = 15;
echo ($age < 18) ? 'Child' : 'Adult';
?>

OUTPUT:
Child
PHP nested if Statement
• The nested if statement contains the if block inside another if block. The inner if statement executes
only when specified condition in outer if statement is true.

<?php
$age = 23;
$nationality = "Indian";
//applying conditions on nationality and age
if ($nationality == "Indian")
{
if ($age >= 18) {
echo "Eligible to give vote";
}
else {
echo "Not eligible to give vote";
}
}
?>

OUTPUT:
Eligible to give vote
PHP switch Statement
• The switch statement is used to perform different actions
based on different conditions.

• Use the switch statement to select one of many blocks of


code to be executed.
PHP switch Statement(contd.)
<?php
$num=20;
switch($num){
case 10:
echo("number is equals to 10");
break;
case 20: OUTPUT:
echo("number is equal to 20");
number is equal to 20
break;
case 30:
echo("number is equal to 30");
break;
default:
echo("number is not equal to 10, 20 or 30");
}
?>
PHP switch statement with character
<?php
$ch = 'k';
switch($ch){
case 'a':
echo 'It is a vowel';
break;
case 'e':
echo 'It is a vowel';
break; OUTPUT:
case 'i':
echo 'It is a vowel';
It is a consonant
break;
case 'o':
echo 'It is a vowel';
break;
case 'u':
echo 'It is a vowel';
break;
default:
echo 'It is a consonant';
}
?>
PHP switch statement with String
<?php
$ch = "B.Tech";
switch ($ch)
{
case "BCA":
echo "BCA is 3 years course";
break;
case "Bsc":
echo "Bsc is 3 years course"; OUTPUT:
break; B.Tech is 4 years course
case "B.Tech":
echo "B.Tech is 4 years course";
break;
case "B.Arch":
echo "B.Arch is 5 years course";
break;
default:
echo "Wrong Choice";
break;
}
?>
PHP switch statement is fall-through
• PHP switch statement is fall-through. It means it will execute all statements after getting the first match, if break
statement is not found.

<?php
$ch = 'c';
switch ($ch)
{
case 'a': OUTPUT:
echo "Choice a";
break;
Choice c
case 'b': Choice d
echo "Choice b"; case a, b, c, and d is not found
break;
case 'c':
echo "Choice c";
echo "</br>";
case 'd':
echo "Choice d";
echo "</br>";
default:
echo "case a, b, c, and d is not found";
}
?>
PHP nested switch statement
<?php
$author = "Stephen King";
$book ="The silence of the lambs";
switch($author)
{
case "JK Rowling":
{
switch($book)
{
case "Harry Potter1";
echo "Harry Potter1, The price is 300$";
break;
case "Harry Potter2";
echo "Harry Potter2, The price is 200$";
break;
OUTPUT:
default:
echo "Author found but not the book"; The silence of the lambs, The
price is 700$
}
}
break;
case "Stephen King":
{
switch($book)
{
case "Hannibal";
echo "Hannibal, The price is 500$";
break;
case "The silence of the lambs";
echo "The silence of the lambs, The price is 700$";
break;
default:
echo "Author found but not the book";
}
}
break;
default:
echo "Author not found";
}
?>

You might also like