Course
PHP Tutorial for Beginners: Learn in 7 Days
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
A control structure is a block of code that decides the execution path of a program depending on the value of the set condition.
Let’s now look at some of the control structures that PHP supports.
Syntax The syntax for if… then… else is;
<?php
if (condition is true) {
block one
else
block two
}
?>HERE,
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
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,
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
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
PHP is a server-side scripting language used to develop static and dynamic websites or web...
PHP Is not fair to compare PHP vs JavaScript, as they both have different purposes for web-site...
Following are frequently asked Laravel and PHP related interview questions for freshers as well as...
What is Ajax? AJAX full form is Asynchronous JavaScript & XML. It is a technology that reduces the...
What is a PHP Array? A PHP array is a variable that stores more than one piece of related data in a...