In this tutorial, you will understand the IF Statement in PHP with examples.

The IF Statement in PHP used to check if the expression is producing a true boolean value or not, to lead the script to another task during the execution.

The IF Statement in PHP can be written as the following.

if ( CONDITION ) {
  ... YOUR CODE HERE
}

It executes the scripts between the two curly braces if the condition between the two braces is achieved. Which is, ignoring the lines between the two curly braces if the condition produced a false boolean value.

IF Statement in PHP

In the following sections, you will see other kinds of PHP IF statements. Let’s dive right in.

The PHP IF Statement with No Curly Braces

The PHP IF statement syntax can be written with no curly braces like the one below.

if ( CONDITION )  
  ... YOUR CODE HERE

So if the condition is producing a true boolean value, then it will execute the following line only. For example.

<?php 
  $is_logged_in = true;
  if ( $is_logged_in ) 
    echo "Welcome Peter !";
?>

But in this syntax, you will be able to write only one statement. So you will not be able to write a lot of lines, only one allowed.

Check the following example will show you the reason.

<?php 
  $is_logged_in = false;
  if ( $is_logged_in ) 
    echo "Welcome Peter !";
    echo "You didn't log in";
?>

The output:

You didn't log in

So the condition in the IF statement is not achieved. But the output was successfully printed, why?

That’s because we have no curly braces, and that allows only one statement to be executed after the last brace in the if condition block.

So, it printed the message because this line was out of the IF condition.

In the following section, I am going to cover the curly braces for the IF condition.

The PHP IF Statement with Curly Braces

On the other hand, the curly brace allows you to write unlimited statements which will stop the execution once it reaches the last curly brace of the syntax. Let’s see how to write the curly braces syntax.

if ( EXPRESSION ) {
  ... YOUR CODE HERE
}

So the if condition will only work until it reaches the last bottom curly brace.

That means it will execute all statements if the expression has a true boolean value. Check the following PHP code example.

<?php 
  $is_logged_in = true;
  if ( $is_logged_in ) {
    echo "Welcome Peter !";
    echo "You logged in";
  }
    
?>

It will print the two statements because they are inside the curly braces and the condition was achieved.

Let’s move to the final kind, which allows you to include the HTML markups inside the if statements.

Embedded Statements and HTML Markups with IF Condition

With this kind, you can embed any code else like HTML, general code statements and so many else. Let’s see how to write this syntax.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Using the IF Statement in PHP</title>
  </head>
  <body>
     <?php if ( true ): ?>
     <!-- Your HTML, ANY STATEMENT ELSE -->
     <?php endif; ?>
  </body>
<html>

So, the condition is ending with the last part called endif. This kind allows you to write any syntax else such as any programming language else, or HTML markups.

Use the Nested IF Statement in PHP

The nested concept means IF condition inside another if condition. Let’s see how it does work.

<?php 
   if ( true ) {
     if( false ) {
       echo "Nothing will print.";
     }
   }
?>

In the next section, I am going to cover how to use the PHP variables with if conditions.

Use variables with IF Statement in PHP

Sometimes you need to assign a specific value to the variable inside the IF condition block. So if the expression is true, it will assign a new value to this variable. Let’s see an example.

<?php 
  if ( true ) {
    $username = "Mohammed";
  }
  echo $username;
?>

It will print the value that was already assigned in the if condition because the condition was achieved. But what about if the condition had a false value?

This will show you an error like the blow.

<?php 
  if ( false ) {
    $username = "Peter";
  }
  echo $username;
?>

The output would be like the following error.

PHP Warning:  Undefined variable $username in /workspace/Main.php on line 6

Wrapping Up

In this tutorial, you understood how to use the IF condition block with examples. Also, you saw many kinds of it, such as:

  • Use IF statement with no curly braces
  • Use IF condition inside another one else.
  • Also, you understood how to use the IF statement with curly braces
  • Nested IF and scope variable.

To learn more about it, read PHP manual.

That’s all, thank you for reading.