---
title: "If…Else Statement in PHP"  
description: "‘If… Else’ statement is used to perform different action for different decision. Conditional (relational operator) statements are used to perform diff"  
author: "Anonymous User"  
published: 2011-09-05  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/717/if-else-statement-in-php  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# If…Else Statement in PHP

‘If… Else’ statement is used to perform different [action](https://yourviews.mindstick.com/view/81713/strict-action-needed-on-counterfeit-drugs-in-india) for different decision. Conditional ([relational](https://www.mindstick.com/blog/10885/equality-relational-and-conditional-operators) [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server)) statements are used to perform different action on basis of different decision. So we can say that ‘If…Else’ statement generally used with conditional statement.\

##### In PHP, we have several conditional statements such as:

‘If’ statement: [If statement](https://www.mindstick.com/forum/23052/using-if-statement-to-check-individual-value-returned-from-a-query) is used to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) line of code at some specified [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition).

##### Let’s we have an example, how to implement ‘If’ statement in php.

##### Syntax:

```
If(condition)   code to be executed if condition is true;
```

##### Example:

```
<html>      <head>        <title> If statement</title>      </head><body>        <?php                            //  Declares variables              $value = 50;                //  if statement block is started at first specified condition               if($value == 50)               {                   //  code for execution                    echo "This is if statement block at (value == 50) condition ";                  }                            //  if statement block is started at second specified condition               if($value == 25)               {                   //  execution code                  echo "This is if statement block at (value == 50) condition ";                    }                         ?></body>  </html>
```

##### Output:

![If…Else Statement in PHP](https://www.mindstick.com/mindstickarticle/a3394e30-37b0-45e9-9e5d-8beffd1ba7dc/images/44ee5082-5054-44c8-9186-ff7d8fbd3bdb.png)

From above example it is clear that if statement block is executed when specified condition will be right.

If…Else Statement: Use If…Else statement when specified condition is true or false. That is If… Else statement executes line of code even; it is in true condition or false condition.

To more understand about If…Else statement, let’s we have an example, how to implement If…else condition.

##### Syntax:

```
If(condition)  code to be executed if condition is true;else  code to be executed if condition is false;
```

##### Example:

```
<html>      <head>        <title> If statement</title>      </head><body>        <?php                            //   retrieve currentmonthname from date()  function              $month =   date("M");                     // if...else statement block is started               if($month == "Aug" )               {                   // if above condition true                    echo "This is if statementblock ";                }                            else                {                  // if above condition not true                 echo " This iselse statement block";                 }         ?></body>  </html>
```

##### Output:

![If…Else Statement in PHP](https://www.mindstick.com/mindstickarticle/a3394e30-37b0-45e9-9e5d-8beffd1ba7dc/images/71d06fcb-0cfb-4004-9260-5f0c2165ee7e.png)

**[Nested if](https://www.mindstick.com/forum/156832/what-is-a-c-sharp-program-to-find-the-largest-among-five-numbers-using-a-nested-if-else-statement)…else Statement:** Use nested if…else statement to [select](https://www.mindstick.com/forum/34066/use-select-operator-in-linq) one of several code [block](https://www.mindstick.com/forum/157599/explain-the-process-control-block-pcb-in-the-operating-system) to be executed.

Let’s we have an example, how to implement nested if…else statement in php.

##### Syntax:

```
If(condition)     code to be executed if condition is true;else if(condition)     code to be executed if condition is true; else if(condition)     code to be executed if condition is true; .....................else     code to be executed if condition is false;
```

##### Example:

```
<html>      <head>        <title> If else if statement</title>      </head><body>        <?php                            //  Declare variables name              $total   = 500 ;              $gain   = 360 ;              $percent   = ($gain/$total )* 100;                 if($percent >=75)               {                     print "Distinction";                 }              else if ($percent >59 && $percent <=74)               {                     print "First division";                 }              else if ( $percent >=45 && $percent <=59)                 {                     print "second Division";                 }              else if ($percent >=33 && $percent <45 )               {                     echo "Third Division";               }              else               {                echo "Fail";               }            ?></body>  </html>
```

##### Output:

![If…Else Statement in PHP](https://www.mindstick.com/mindstickarticle/a3394e30-37b0-45e9-9e5d-8beffd1ba7dc/images/bfdd6af2-443d-4013-8edc-6fc419292977.png)

\

---

Original Source: https://www.mindstick.com/articles/717/if-else-statement-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
