If Else statement किसी भी programming language का सबसे important feature है , PHP If Else conditions के according किसी Code Of Block को run करने के लिए use होता है।  Means जब हमें Condition True होने पर कोई दूसरा code run करना हो Condition गलत होने पर कुछ और तब हम If Else का use करते हैं।

PHP IF Else Syntax

if(condition)
{
  // write your logic for true condition
}
else
{
  //write logic if condition false
}

Example - 

File : ifelse.php

Copy Fullscreen Close Fullscreen
<?php 
	$age  = 19;
	if($age > 18)
	{
	      echo 'You are eligible to vote';
	}
        else
        {
              echo 'You are not eligible to vote';
        }
?>
Output
You are able to vote

तो ऊपर दिए गए example में आप देख सकते हैं की PHP में If Else किस तरह से use करते हैं। हालांकि PHP में if या else के साथ use होने वाले curly brackets {} की जगह आप colon : भी use कर सकते हैं।

Example -

File : ifelse2.php

Copy Fullscreen Close Fullscreen
<?php 
	$age  = 19;
	if($age > 18) :
	      echo 'You are eligible to vote';
        else:
              echo 'You are not eligible to vote';
        endif
?>
Output
You are able to vote

If Else With HTML


जब हम किसी project / website develop करते हैं , तो हमें PHP को दूसरी languages (HTML, CSS , JavaScript , j Query etc .) के साथ एक्सेक्यूटे करना पड़ता हैं , इस कंडीशन में कुछ इस तरह से हम If Else use करते हैं।

Example -

File : ifelse3.php

Copy Fullscreen Close Fullscreen
 <?php 
	if($condition) :
  ?>
	      Your HTML Content
  <?php
        else:
  ?>
              Another HTML Content
  <?php
        endif
  ?>
Output
Another HTML Content

तो इस तरह से हम If Else Statement के साथ HTML को use करते हैं। Example में मैंने एक undefined variable $condition check किया है हालाँकि यह undefined है इसलिए condition False है।

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