Tutorial Title: Control Structures in PHP
Course: PHP
Instructor: Muhammad Samim
Course: PHP
Instructor: Muhammad Samim
1. If, elseif and else statements in PHP
- These are the conditions which are applied to control the flow of program
if(3<2) { echo "First condition";}
elseif (3>10) {echo "Second condition;}
else {echo "all conditions are false";}
//"all conditions are false" will be displayed because because above both conditions are false
2. Comparison operators in PHP
2. Comparison operators in PHP
- Equal == //e.g 3 == "3" This condition will be true, datatypes are change but values are same
- Identical === //To identify if both values are same and of same datatype e.g 3 === "3" This condition will be false because values are same but datatypes are change
- Less than <
- Greater than >
- Less than or Equal to <=
- Greater than or Equal to >=
- Less than or Greater than <>
- Not Equal !=
- Not Identical !==
3. Logical Operators in PHP
- And &&
- Or ||
- Not !
- e.g if (6 === "6" && 6 < 10){echo "Condition is true";} //It will not be displayed because one condition is false in && case both conditions must be true
- e.g if (6 === "6" || 6 < 10){echo "Condition is true";} //It will be displayed because in both conditions one is true
4. Switch Statement in PHP
- It works as if statement does but here we can compare multiple values against one condition
$variableName = 30;
switch($variableName){
case 24:
echo "This is 24";
break;
case 30:
echo "This is 30";
break;
case 50:
echo "This is 50";
break;
default:
echo "All cases are false";
break;
}
//"This is 30" will be displayed because 30 is equal to the value of variable.
5. While Loop in PHP
5. While Loop in PHP
- Its used to to repeat anything in program
$variableName = 0;
while($variableName <= 10){
echo $variableName . "<br>";
$variableName ++;
}
//0 to 10 numbers will be displayed
$variableName = 0;
while($variableName <= 10){
echo "This is nightLife<br>";
$variableName ++;
}
//11 times "This is nightLife" will be displayed
6. For Loop in PHP
for($variableName = 0; $variableName <=10; $variableName ++){
echo $variableName . "<br>";
}
//0 to 10 numbers will be displayed
7. Foreach Loop
foreach($arrayName as $variableName){
echo $variableName . "<br>";
}
//It will displayed all elements of array
6. For Loop in PHP
- Its same like While Loop but in For Loop we can create variable, set the condition and increment in variable in one line inside For Loop.
for($variableName = 0; $variableName <=10; $variableName ++){
echo $variableName . "<br>";
}
//0 to 10 numbers will be displayed
7. Foreach Loop
- Foreach Loop is used for arrays to access each element of them
foreach($arrayName as $variableName){
echo $variableName . "<br>";
}
//It will displayed all elements of array
Comments
Post a Comment