IF-Else: Executes the IF-Else Statement in PHP

Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if X condition is true? That is where PHP’s if-else block steps in. if-else helps you control the flow of your program.

    In this tutorial, you will understand how the if-else block works with examples.

    What Exactly Is an If-Else Block?

    The else block is the final part of an if statement, executed when none of the other conditions are true.

    Think of the if-else block as the decision-making engine of your PHP code. It allows you to define, “If this condition is met, take this action; otherwise, handle it differently.”

    Here is the basic structure:

    if (condition) {
        
    } else {
        // Code to run if the condition is false
    }

    So, the if part checks a condition. If it is false, the else runs a code in else {} block.

    Let’s say you are creating a website that greets visitors based on their login status. Here is how you would do it:

    $isLoggedIn = true;
    
    if ($isLoggedIn) {
        echo "Welcome back!";
    } else {
        echo "Please log in to continue.";
    }
    • If $isLoggedIn is true, your user gets a warm “Welcome back!” message.
    • Otherwise, they will need to log in.

    Let’s move on to the following section to see more examples.

    Examples of PHP if else Block

    Else with No Curly Braces

    Using this syntax, you are limited to a single statement—trying to include multiple statements without curly braces will not work.

    $is_logged = false;
    if ( $is_logged ) 
      echo "Welcome to flatcoding.com";
    else 
      echo "Invalid Username or Password";

    You can also use it with other languages like JavaScript, HTML, and more. Let’s see how you can do that in the below section.

    PHP IF Else with Embedded Syntax

    Here, you can mix PHP code with HTML markup or other content. If the condition does not pass, it outputs something different. Take a look at this example:

    <!DOCTYPE html>
    <html>
      <head>
        <title>The PHP IF ELSE</title>
        <?php if ( false ): ?>
        <?php else: ?>
           <script type="javascript">alert("Hello World");</script>
        <?php endif;?>
      </head>
      <body>
         <h1>Welcome to Home Page !</h1>
      </body>
    </html>

    In this example, the JavaScript code runs when the main if statement does not meet the condition.

    Let’s summarize it

    Wrapping Up

    The if-else statement is used to execute PHP statements when the if condition fails to evaluate to the true boolean value.

    Here is a quick example:

    if ( false ) {
      
    } else {
      // This block executes once the if condition fails to execute.
    }

    Additionally, the if-else statement can be written with curly braces, without braces, or with embedded syntax, and here you can use it with any other language as well.

    Thank you for reading. Happy Coding!

    Similar Reads

    PHP Strings: Types, Variables & Syntax Tips

    If you start working with PHP, it won't take you that long to figure out that strings are everywhere. Besides…

    PHP array_merge: Combine Multiple Arrays into One

    You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…

    PHP DOMDocument: XML DOM Parser Guide

    In PHP, if you're working with XML files, you’ll probably work with DOMDocument. It is a powerful class, enabling you…

    Learn How to Insert Documents in MongoDB Using PHP

    Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…

    PHP array_column: How to Extract Values from Arrays

    The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…

    PHP if-elseif Statement: ‘elseif’ versus ‘else if’

    PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…

    PHP MySQL ORDER BY: How to Sort Data in SQL?

    Sorting data is one of those things we do all the time but rarely stop to think about. Whether you…

    PHP echo vs print: Key Differences & Usage Guide

    Echo and print are foundational in displaying output in PHP, and though they might be similar in some ways, they…

    PHP array_pop: Remove and Return the Last Element of an Array

    The array_pop function in PHP gives you a direct way to take the last element from an array. Understand the…

    How to Update MySQL Data with PHP?

    Understanding how to update data in PHP and MySQL is like editing a draft—it is all about tweaking the right…

    Previous Article

    PHP if-elseif Statement: 'elseif' versus 'else if'

    Next Article

    PHP While Loop: Iterating Through Code Blocks

    Write a Comment

    Leave a Comment

    Your email address will not be published. Required fields are marked *


    Subscribe to Get Updates

    Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
    No spam. Unsubscribe anytime.