Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

PHP If Else


PHP if else statement is used to test condition. There are various ways to use if statement in PHP.

  • if
  • if-else
  • if-else-if
  • nested if

PHP If Statement

PHP if statement is executed if condition is true.

Syntax

  1. if(condition){
  2. //code to be executed
  3. }

Flowchart

if1

Example

  1. <?php
  2. $num=12;
  3. if($num<100){
  4. echo “$num is less than 100”;
  5. }
  6. ?>

Output:

12 is less than 100

PHP If-else Statement

PHP if-else statement is executed whether condition is true or false.

Syntax

  1. if(condition){
  2. //code to be executed if true
  3. }else{
  4. //code to be executed if false
  5. }

Flowchart

if2

Example

  1. <?php
  2. $num=12;
  3. if($num%2==0){
  4. echo “$num is even number”;
  5. }else{
  6. echo “$num is odd number”;
  7. }
  8. ?>

Output:

12 is even number

Comments

comments