0% found this document useful (0 votes)
33 views8 pages

PHP If Else

Control structures like if/else and switch/case determine the flow of program execution based on conditions. If/else has an if block that runs if the condition is true and an else block that runs if false. Switch/case selects one of multiple case blocks to run based on a condition's value, or a default if no cases match. These structures are useful for selecting code paths based on Boolean logic or a variable's value.

Uploaded by

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

PHP If Else

Control structures like if/else and switch/case determine the flow of program execution based on conditions. If/else has an if block that runs if the condition is true and an else block that runs if false. Switch/case selects one of multiple case blocks to run based on a condition's value, or a default if no cases match. These structures are useful for selecting code paths based on Boolean logic or a variable's value.

Uploaded by

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

A control structure is a block of code that decides the execution path of a

program depending on the value of the set condition.

 Sequential – this one involves executing all the codes in the order in which
they have been written.
 Decision – this one involves making a choice given a number of options. The
code executed depends on the value of the condition.

PHP IF Else
If… then... else is the simplest control structure. It evaluates the conditions
using Boolean logic
When to use if… then… else

 You have a block of code that should be executed only if a certain condition is
true
 You have two options, and you have to select one.
 If… then… else if… is used when you have to select more than two options
and you have to select one or more

Syntax The syntax for if… then… else is;

<?php

if (condition is true) {

block one

else

block two
}

?>

  HERE,

 “if (condition is true)” is the control structure


 “block one” is the code to be executed if the condition is true
 {…else…} is the fallback if the condition is false
 “block two” is the block of code executed if the condition is false

How it works The flow chart shown below illustrates how the if then… else control
structure works

Let’s see this in action The code below uses “if… then… else” to determine the
larger value between two numbers.
<?php

$first_number = 7;

$second_number = 21;

if ($first_number > $second_number){

echo "$first_number is greater than $second_number";

}else{

echo "$second_number is greater than $first_number";

?>

Output:
21 is greater than 7

PHP Switch Case


Switch… case is similar to the if then… else control structure.
It only executes a single block of code depending on the value of the condition.

If no condition has been met then the default block of code is executed.

It has the following basic syntax.

<?php

switch(condition){

case value:

//block of code to be executed

break;

case value2:

//block of code to be executed


break;

default:

//default block code

break;

?>

 
HERE,

 “switch(…){…}” is the control structure block code


 “case value: case…” are the blocks of code to be executed depending on the
value of the condition
 “default:” is the block of code to be executed when no value matches with the
condition

How it works

The flow chart shown below illustrates how the switch control structure works
Practical example

The code below uses the switch control structure to display a message depending on
the day of the week.

<?php

$today = "wednesday";
switch($today){

case "sunday":

echo "pray for us sinners.";

break;

case "wednesday":

echo "ladies night, take her out for dinner";

break;

case "saturday":

echo "take care as you go out tonight.";

break;
default:

echo "have a nice day at work";

break;

?>

Output:

ladies night, take her out for dinner

Summary

 Control structures are used to control the execution of the program


 The if then... else is when you have more than route block of code to execute
depending on the value of the condition
 Switch… case is used to when you have a number of block codes, and you only
have to execute one of them depending on the value of the set case.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy