PHP Conditional Statements
Conditional statements are used to control flow of a program.
Example -
Example -
if statement, if-else statement, Alternative if-else statement and switch statement
.if statement:
The if statement is used to execute some code only if a specified condition is true.Syntax
<?php?>Note
PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the curly braces is executed, else it will be ignored.Example
<?php?>Output
10Example
<?php?>Output
Condition is false ,exit from if satement output is blank.PHP - The if...else Statement
We use if-else statement to execute some code if a condition is true and another code if the condition is false.
if statement:
The if statement is used to execute some code only if a specified condition is true.Syntax
Note
PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the if curly braces is executed, else the code inside the else curly braces is executed.Example
<?php $x = 5;
if($x > 10)
{
echo $x; //print 5
}
else
{
$x += 7;
echo $x; // Will print 12
}
?>Output
if condition $x>10 is true than output = 5if condition $x>10 is false than output = 12
Using if-else with more than one condition is:
Example
Below example represents if-else with more than one condition :
<?php?>Output
if condition $x>10 is true than output = 5if condition $x == 5 is true than output = 12
Otherwise output = 6
PHP Switch statement:
The switch statement is wondrous and magic.
With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do this.
With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do this.
PHP switch statement example
In our example the single variable will be $day and the cases will be: sunday, munday, tuesday, wednesday, thursday, friday, saturday and the sunday.
PHP CODE :
<?php $day = "tuesday";
switch ($day)
{
case "sunday":
echo "today is sunday";
break;
case "monday":
echo "today is monday";
break;
case "tuesday":
echo "today is tuesday";
break;
case "wednesday":
echo "today is wednesday";
break;
case "thursday":
echo "today is thursday";
break;
case "friday":
echo "today is friday";
break;
case "saturday":
echo "today is saturday";
break;
default :
echo "invalid entry";
break;
}
?>Output
if value of variable $day is "sunday" then output is : today is sundayif value of variable $day is "monday" then output is : today is monday
if value of variable $day is "tuesday" then output is : today is tuesday
if value of variable $day is "wednesday" then output is : today is wednesday
if value of variable $day is "thursday" then output is : today is thursday
if value of variable $day is "friday" then output is : today is friday
if value of variable $day is "saturday" then output is : today is saturday
if value of variable $day does not match any case then execute defauly output is : invalid entry
No comments:
Post a Comment