PHP ElseIf / Else If In Hindi

📔 : PHP 🔗

elseif / else if  जैसा की नाम से पता  चलता है कि अगर upper  condition condition false होती है और  elseif में दी हुयी condition true return करती  है तो elseif code  of block run होगा। हालाँकि if के बाद यह जरूरी नहीं की एक ही elseif use हो Condition के according एक से ज्यादा भी use हो सकते हैं।

PHP elseif / else if Syntax

if(condition)
{
  // write your logic for true condition
}
elseif(another condition)
{
  //write logic if first condition gets false.
}
else
{
  //if both conditions get false
}

Example -

File : ifelse.php

Copy Fullscreen Close Fullscreen
<?php 
   $marks = 70;
   if($marks >= 80)
   {
      echo "Amazing Grade : A";
   }
   elseif($marks >= 70)
   {
      echo "Grade B";
   }
   elseif($marks >= 50)
   {
      echo "Grade C";
   }
   elseif ($marks >= 33) 
   {
      echo "Echo D";   
   }
   else 
   {
      echo "Failed !";
   }
?>
Output
Grade B

दिए गए Example में आप देख सकते हैं कि किस तरह से आप elseif Use कर सकते हैं। हालाँकि elseif को एक साथ लिखने की जगह else if दो शब्दों में अलग अलग भी लिख सकते हैं।

Example -

File : ifelse2.php

Copy Fullscreen Close Fullscreen
<?php 
   $marks = 70;
   if($marks >= 80)
   {
      echo "Amazing Grade : A";
   }
   else if($marks >= 70)
   {
      echo "Grade B";
   }
   else if($marks >= 50)
   {
      echo "Grade C";
   }
   else if ($marks >= 33) 
   {
      echo "Echo D";   
   }
   else 
   {
      echo "Failed !";
   }
?>
Output
Grade B

Note - Else If statement में आप curly brackets {} के साथ ही else if अलग अलग लिख सकते हैं , अगर आप else if colon : के साथ use करते हैं तो Error आएगी।

Example -

File : ifelse3.php

Copy Fullscreen Close Fullscreen
<?php 
   $a = 14;
   $b = 11;

   /*Correct way to use elseif with colon(:) */
   if($a > $b):
    echo "a is greater than b";
   elseif($a==$b):
    echo "both are equal";
   endif;

   /*Incorrect way*/
   if($a>$b):
    echo "a is greater than b";
   else if($a==$b):
    echo "It'll generate error";
   endif;
?>
Output
Parse error: syntax error, unexpected 'if' (T_IF), expecting ':' in C:\xampp\htdocs\test\ifelse3.php on line 15

clearly देख सकते हैं कि line 15 पर else if दो शब्दों में लिखा होने की वजह से fatal error generate हुए है। PHP में Else If statement use करते हैं यह बात ध्यान में रखें कि colon (:) के साथ कभी भी else if अलग अलग न हो।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers