PHP Control statement
In this tutorial I will show you many php control statement in php programming language. after start program in php you need to some knowledge of php control statement.
if-else Statement
This is a condition checking control statement. If the condition is true, then some statement will work and if the condition is false then another some statement will work.
Syntax :
<?php if(condition) { true statements; } else { false statement; } ?>
Example of if-else :
Checking a number is even or odd.
<?php $number = 23; if($number%2==0) // double equal is a operator for checking equality. { echo $number.' Number is Even'; //if condition true then this statement will execute. } else { echo $number.' Number is Odd'; // execute this statement, when the condition will be false } ?>
Switch :
Switch is a statement, which performs different on different condition.
Syntax :
switch (n) {
case label1:
statement will execute if n value is label1
break;
case label2:
statement will execute if n value is label2
break;
case label3:
statement will execute if n value is label3
break;
…
default:
if all the above condition if false, then this condition will execute.
}
For example
<?php $str = 'mobile'; switch($str) { case "mobilewe": echo "This is a Laptop"; break; case "mobile": echo "This is a TV"; break; case "Tab": echo "This is a Tab"; break; default: echo "This is a electronic goods"; } ?>
Here $str is a string variable. If the $str variable matched to the case, then that case’s statements will execute and after execution that statements, break keyword breaks the execution of next case. If we remove the break keyword, first matched case’s statement work and the next all case’s statements including default will be executed.
Loop
This statement is used to execute statements till the condition is false. Four types of loop available in php, for loop, which loop , do-while loop and foreach loop.
for loop
Syntax :
for(initialization, condition, increment / decrement)
{
statements;
}
for example :
<?php for($i=1;$i<=10;$i++) { echo $i.'<br>'; } ?>
This program output will be 1 to 10. First initialize the $i value to 1. then condition is $i less than 10. if the condition is true then print $i value(statement) and increment it. When the $i value becomes 10 then condition will be false and the loop will be stopped. the output is the statements run 10 times.
While Loop :
Syntax :
initialization;
while(condition)
{
statements;
increment / decrement;
}
Example :
<?php $i=1; //initialization while($i<10) //condition { echo $i.'<br>'; $i++; //increment or decrement } ?>
This loop is same as for loop , only the syntax is difference.
Do-While Loop :
There is one little bit difference in this loop.
syntax :
initialization;
do{
statements.
increment / decrement;
}while(condition);
This loop runs statement first, then check condition. if the condition is false then the statements will be run for once.
for example.
<?php $i=1; //initialization do{ echo $i.'<br>'; $i++; //increment or decrement }while($i<10); //condition ?>
This program output 1 to 10
Next Example :
<?php $i=1; //initialization do{ echo $i.'<br>'; '; $i++; //increment or decrement }while($i>10); //condition ?>
According to initialization the condition is false. But the statements will be run once. so, the output will be 1.