php conditional statements

if statement

इस statement में code तभी execute होगा जब if statement की condition True होगी| if statement एक conditional statement है जिसमे कई प्रकार की condition check करि जाती है|| if statement में अगर condition true होती है तभी वह if statement की body statement (code) execute होगा otherwise नहीं होगा.

syntax:

if(condition){
 //execute code if condition is true;
}

Example:

<?php

 $a = 4;
 $b = 5;

 if($a < $b)
 {
  echo $a."is less than".$b
  //here condition is true
 }

?>

ऊपर के example में देख सकते है की $a=4 or $b=5 में $b की value ज्यादा है $a से और अमरा if statement वह define करता है की $a की value $b से कम होगी तो if statement की body execute होगी|.

If…Else statement

If…else statement में अगर if statement की condition true होती है तो if statement की body statement(code) executes होगा और अगर if statement की condition false होती है तो else statement का body code execute होगा.

syntax:

if(condition){
  code executed if  condition is TRUE
}
else{
  code to be executed if condition is FALSE
}

Example:

<?php
$a = 4;
$b = 5;
if($a < $b){
    echo "your condition is TRUE";
}
else {
   echo "this block of code execute when if statement condition is False";
}
?>

ऊपर दिए गए example से समजते है अगर if statement की condtion क्या कहती है अगर $b की value $a से बड़ी यानि ज्यादा होगी तो if statement का code execute होगा नहीं तो else statement का code execute होगा.

else if statement

else if statement का उपयोग multiple condition define करने के लिए किया जाता है| लेकिन elseif statement कि multiple condition में से एक बी condition true होती है तो वह दूसरे सारी condition को skipt कर देता है यानि की दूसरी किसी बी condition को execute नहीं करता.

syntax:

if(condition){
   statement1 block code executed
} elseif(condition){
   statement2 block code executed
}else{
   statement else block code executed
}

another way define syntax:

<?php
if(condition):
   statement1 block code executed
elseif(condition):
   statement2 block code executed
else:
   statement else block code executed
endif:
?>

Example:

<?php
$a = 10;
if($a>20){
    // if condition is TRUE
    echo "20 is greterthan".$a;
}elseif($a==10){
    echo "10 is equal to".$a;
} else {
    echo"execut this code when all condition is FALSE";
}

?>

Leave a Comment

Your email address will not be published. Required fields are marked *