Decision Making Statements in PHP

Decision Making Statements in PHP

Decision Making Statements

PHP हमें कुछ प्रकार की Condition के आधार पर कार्य करने की अनुमति देता है जो  logical or comparative हो सकती हैं। इन Conditions के Results के base पर, या तो TRUE या FALSE,  यह two- way path की तरह है। कुछ चाहिए तो इस तरफ जाओ वरना उस तरफ मुड़ो। इस सुविधा का उपयोग करने के लिए, PHP हमें Five conditional statements प्रदान करता है:

  • if statement
  • if…else statement
  • if…elseif…else statement
  • nested if Statement

  • switch statement

If Statement

PHP if Statement Code के conditional execution की अनुमति देता है। यदि Condition True है तो इसे execute किया जाता है।

यदि Statement का उपयोग Code के Block को execute करने के लिए किया जाता है, तो if Statement के अंदर Present होता है, यदि specified condition True है।

Syntax

if(condition)
{
 //code to be executed 
}

Example

<?php  
$num=12;  
if($num<100){  
echo "$num is less than 100";  
}  
?>

Output:

12 is less than 100

PHP If-else Statement

PHP if-else statement execute किया जाता है कि क्या Condition True है या False है।

if-else statement if Statement से थोड़ा different है। यदि specified condition True है तो यह Code के एक Block को execute करता है और यदि condition False है तो Code का another block execute करता है।

Syntax

if(condition)
{  
   //code to be executed if true
  }
else
{  
   //code to be executed if false 
 }

Example

<?php  
$num=12;  
if($num%2==0){  
echo "$num is even number";  
}else{  
echo "$num is odd number";  
}  
?>  

 

Output

12 is even number

PHP If-else-if Statement

PHP if-else-if एक special statement है जिसका उपयोग  multiple if else statements को combine करने के लिए किया जाता है। इसलिए, हम इस Statement का उपयोग करके multiple conditions की check कर सकते हैं।

Syntax

if (condition1){    
//code to be executed if condition1 is true    
} elseif (condition2){      
//code to be executed if condition2 is true    
} elseif (condition3){      
//code to be executed if condition3 is true    
....  
}  else{    
//code to be executed if all given conditions are false    
}

 

Flowchart

if (condition1){    
//code to be executed if condition1 is true    
} elseif (condition2){      
//code to be executed if condition2 is true    
} elseif (condition3){      
//code to be executed if condition3 is true    
....  
}  else{    
//code to be executed if all given conditions are false    
}

Example

<?php  
    $marks=69;      
    if ($marks<33){    
        echo "fail";    
    }    
    else if ($marks>=34 && $marks<50) {    
        echo "D grade";    
    }    
    else if ($marks>=50 && $marks<65) {    
       echo "C grade";   
    }    
    else if ($marks>=65 && $marks<80) {    
        echo "B grade";   
    }    
    else if ($marks>=80 && $marks<90) {    
        echo "A grade";    
    }  
    else if ($marks>=90 && $marks<100) {    
        echo "A+ grade";   
    }  
   else {    
        echo "Invalid input";    
    }    
?>

PHP nested if Statement

nested if Statement में if block होता है तो दूसरे के अंदर if block होता है।  inner if statement केवल तभी execute होता है जब outer में specified condition if Statement True है।

Syntax

if (condition) {    
//code to be executed if condition is true   
if (condition) {    
//code to be executed if condition is true    
}    
}

Example

<?php  
               $age = 23;  
    $nationality = "Indian";  
    //applying conditions on nationality and age  
    if ($nationality == "Indian")  
    {  
        if ($age >= 18) {  
            echo "Eligible to give vote";  
        }  
        else {    
            echo "Not eligible to give vote";  
        }  
    }  
?>

Output:

Eligible to give vote

Switch Statement

PHP Switch Statement का उपयोग multiple conditions से एक Statement को execute करने के लिए किया जाता है। यह PHP if-else-if Statement की तरह काम करता है।

Syntax

switch(expression){      
case value1:      
 //code to be executed  
 break;  
case value2:      
 //code to be executed  
 break;  
......      
default:       
 code to be executed if all cases are not matched;    
}

Important points to be noticed about switch case:

  • Default एक optional statement है। यह भी Important नहीं है कि default हमेशा last statement. होना चाहिए।
  • Switch Statement में केवल एक default हो सकता है। एक से अधिक default के कारण Fatal error. हो सकती है।
  • प्रत्येक case में एक ब्रेक Break Statement हो सकता है, जिसका उपयोग Statement के sequence को terminate  करने के लिए किया जाता है।
  • Break Statement Switch में उपयोग करने के लिए optional है। यदि Break का उपयोग नहीं किया जाता है, तो मिलान किए गए case value. को खोजने के बाद सभी statement execute होंगे।
  • PHP आपको Switch expression में number, character, String के साथ-साथ Functions का उपयोग करने की अनुमति देता है।
  • स्विच स्टेटमेंट के नेस्टिंग की अनुमति है, लेकिन यह प्रोग्राम को अधिक जटिल और कम पठनीय बनाता है।
  • आप Colon (:) के बजाय Semicolon (;) का उपयोग कर सकते हैं। यह कोई error उत्पन्न नहीं करेगा।

Example

<?php      
$num=20;      
switch($num){      
case 10:      
echo("number is equals to 10");      
break;      
case 20:      
echo("number is equal to 20");      
break;      
case 30:      
echo("number is equal to 30");      
break;      
default:      
echo("number is not equal to 10, 20 or 30");      
}     
?>

Output

number is equal to 20
Previous articleWhat is PHP
Next articleOperators in PHP

LEAVE A REPLY

Please enter your comment!
Please enter your name here