CN5109 WEB APPLICATION DEVELOPMENT Chapter 2 PHP – Conditional and Loop Statements
Conditional Statements • Conditional statements are used to perform different actions based on different conditions. • It perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
Conditional Statements • The following are conditional statements in PHP: • if statement - executes some code if one condition is true • if...else statement - executes some code if a condition is true and another code if that condition is false • if...elseif....else statement - executes different codes for more than two conditions • switch statement - selects one of many blocks of code to be executed
IF Statement • The if statement executes some code if one condition is true. Syntax: if (condition) { code to be executed if condition is true; }
IF Statement • Example: <?php $t = 15; if ($t < 20) { echo "Have a good day!"; } ?> Output:
IF … ELSE Statement • The if....else statement executes some code if a condition is true and another code if that condition is false. Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
IF … ELSE Statement • Example: <?php $t = 15; if ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> Output:
IF … ELSEIF … ELSE Statement • The if....elseif...else statement executes different codes for more than two conditions. Syntax: if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true; } else { code to be executed if all conditions are false; }
IF … ELSEIF … ELSE Statement • Example: <?php $t = 5; if ($t < 10) { echo "Have a good morning!"; } elseif ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> Output:
SWITCH Statement • The switch statement is used to perform different actions based on different conditions. Syntax: switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
SWITCH Statement • Example: <?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?> Output:
Quiz a. What do CONDITIONAL STATEMENTS used for? b. List down any four examples of CONDITIONAL STATEMENTS.
Quiz Write a PHP code using Conditional Statement: If AGE below 18, display message “You cannot enter.” If MARKS more than 39, display message “You Passed!” Else, display message “You Failed!” Write one example using SWITCH Statement
Loop Statements • A loop statement in programming performs predefined tasks while or until a predetermined condition is met. • The following looping statements in PHP: • while - loops through a block of code as long as the specified condition is true • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true • for - loops through a block of code a specified number of times • foreach - loops through a block of code for each element in an array
WHILE Loop • The while loop executes a block of code as long as the specified condition is true. Syntax: while (condition is true) { code to be executed; }
WHILE Loop Example: <?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> Output:
DO … WHILE Loop • The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. Syntax: do { code to be executed; } while (condition is true);
DO … WHILE Loop Example: <?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> Output:
FOR Loop • The for loop is used when you know in advance how many times the script should run. • Parameters: • init counter: Initialize the loop counter value • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. • increment counter: Increases the loop counter value Syntax: for (init counter; test counter; increment counter) { code to be executed; } condition is true);
FOR Loop Example: <?php for ($x = 20; $x <= 15; $x++) { echo "The number is: $x <br>"; } Echo “End loop”; ?> Output:
FOREACH Loop • The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax: foreach ($array as $value) { code to be executed; }
FOREACH Loop Example: <?php $numbers = array(1,45,3,90); foreach ($numbers as $value) { echo "$value <br>"; } ?> Output:
Quiz a. Define LOOP STATEMENT. b. Explain the difference between WHILE Statement and DO … WHILE Statement. c. Write a PHP code using FOR Loop Statement to display number 10 to 30.

Web Application Development using PHP Chapter 2

  • 1.
    CN5109 WEB APPLICATION DEVELOPMENT Chapter 2 PHP– Conditional and Loop Statements
  • 2.
    Conditional Statements • Conditionalstatements are used to perform different actions based on different conditions. • It perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
  • 3.
    Conditional Statements • Thefollowing are conditional statements in PHP: • if statement - executes some code if one condition is true • if...else statement - executes some code if a condition is true and another code if that condition is false • if...elseif....else statement - executes different codes for more than two conditions • switch statement - selects one of many blocks of code to be executed
  • 4.
    IF Statement • Theif statement executes some code if one condition is true. Syntax: if (condition) { code to be executed if condition is true; }
  • 5.
    IF Statement • Example: <?php $t= 15; if ($t < 20) { echo "Have a good day!"; } ?> Output:
  • 6.
    IF … ELSEStatement • The if....else statement executes some code if a condition is true and another code if that condition is false. Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • 7.
    IF … ELSEStatement • Example: <?php $t = 15; if ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> Output:
  • 8.
    IF … ELSEIF… ELSE Statement • The if....elseif...else statement executes different codes for more than two conditions. Syntax: if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true; } else { code to be executed if all conditions are false; }
  • 9.
    IF … ELSEIF… ELSE Statement • Example: <?php $t = 5; if ($t < 10) { echo "Have a good morning!"; } elseif ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> Output:
  • 10.
    SWITCH Statement • Theswitch statement is used to perform different actions based on different conditions. Syntax: switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
  • 11.
    SWITCH Statement • Example: <?php $favcolor= "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?> Output:
  • 12.
    Quiz a. What doCONDITIONAL STATEMENTS used for? b. List down any four examples of CONDITIONAL STATEMENTS.
  • 13.
    Quiz Write a PHPcode using Conditional Statement: If AGE below 18, display message “You cannot enter.” If MARKS more than 39, display message “You Passed!” Else, display message “You Failed!” Write one example using SWITCH Statement
  • 14.
    Loop Statements • Aloop statement in programming performs predefined tasks while or until a predetermined condition is met. • The following looping statements in PHP: • while - loops through a block of code as long as the specified condition is true • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true • for - loops through a block of code a specified number of times • foreach - loops through a block of code for each element in an array
  • 15.
    WHILE Loop • Thewhile loop executes a block of code as long as the specified condition is true. Syntax: while (condition is true) { code to be executed; }
  • 16.
    WHILE Loop Example: <?php $x =1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> Output:
  • 17.
    DO … WHILELoop • The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. Syntax: do { code to be executed; } while (condition is true);
  • 18.
    DO … WHILELoop Example: <?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> Output:
  • 19.
    FOR Loop • Thefor loop is used when you know in advance how many times the script should run. • Parameters: • init counter: Initialize the loop counter value • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. • increment counter: Increases the loop counter value Syntax: for (init counter; test counter; increment counter) { code to be executed; } condition is true);
  • 20.
    FOR Loop Example: <?php for ($x= 20; $x <= 15; $x++) { echo "The number is: $x <br>"; } Echo “End loop”; ?> Output:
  • 21.
    FOREACH Loop • Theforeach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax: foreach ($array as $value) { code to be executed; }
  • 22.
    FOREACH Loop Example: <?php $numbers =array(1,45,3,90); foreach ($numbers as $value) { echo "$value <br>"; } ?> Output:
  • 23.
    Quiz a. Define LOOPSTATEMENT. b. Explain the difference between WHILE Statement and DO … WHILE Statement. c. Write a PHP code using FOR Loop Statement to display number 10 to 30.