Skip to main content

Control Structures in PHP

Tutorial Title: Control Structures in PHP
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

  • 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

  • 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

  • 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
$arrayName = array(1232,4534,565,678,987);
foreach($arrayName as $variableName){
echo $variableName . "<br>";
}
//It will displayed all elements of array

Comments

Popular posts from this blog

External Page Submission/Submission to another page

Tutorial Title:   External Page Submission/Submission to another page Course: PHP Instructor: Muhammad Samim 1. Submitting action from one page to another page 2. First make one page with name page1.php <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Document</title> </head> <body>      <form action="page2.php" method="post"> //It will direct from page1.php to page2.php           <br><input type="text" placeholder="Email" name="anyNameForEmail">     <br><input type="password" placeholder="Password" name="anyNameForPassword">     <br><input type="submit" name="click">    </form>   </body> </html> 3. Second make another file with name page2.php <?php if(isset($_POST["click"])){     $anyNameO...

Extracting Information from form

Tutorial Title:  Extracting Information from form Course: PHP Instructor: Muhammad Samim 1. <?php if(isset($_POST["anyName"])) { $anyNameOfVariableForEmail = $_POST["anyNameForEmail"]; //Now because of it email must be typed otherwise it will not work $anyNameOfVariableForPassword = $_POST["anyNameForPassord"];//Now because of it password must be typed otherwise it will not work echo $anyNameOfVariableForEmail; //It will display the email echo $anyNameOfVariableForPassword; //It will display the password } ?> <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Document</title> </head> <body>      <form action="21form.php" method="post">        <input type="text" placeholder="Email" name="anyNameForEmail">        <input type="password" placeholder="...