Conditionals: Free PHP & MySQL Tutorial

Improve your programming skills with our comprehensive PHP & MySQL tutorial, covering topics such as If/Else statements, Elseif statements, Switch statements, and various operators, accompanied by practical exercises for a better understanding of the subject.

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Note: These materials are provided to give prospective students a sense of how we structure our class exercises and supplementary materials. During the course, you will get access to the accompanying class files, live instructor demonstrations, and hands-on instruction.

Topics covered in this PHP & MySQL tutorial:

If/Else statements, Elseif statements, Switch statements, Comparison operators, Logical operators, The difference between == & ===

Exercise Overview

Conditional operators will be one of the most-used elements of your programming life. Simply put, conditional operators are a way to choose when certain things happen. For example, if something is true, then do something. If it is not true, do something else.

SQL Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

If/Else Statements

An if/else statement is one of the simplest and most-used pieces of logic you’ll find in any programming language. Let’s explore how they work in PHP.

The basic syntax for a PHP if statement is as follows:

if (something is true) {
   do something
}
  1. In your code editor, open ifelse.php from the phpclass folder.

  2. Let’s create two simple variables and see if they are equal. In between the <body> tags add the following bold code:

    <?php 
       $a = 8;
       $b = 8;
    
       if ($a == $b) {
          echo "a is equal to b";
       }
    ?>
    

    First we set two variables, both equal to 8. Then we test to see if they are equal. Note that to set a variable you use one = sign, and to test if they are equal, you use two ==. (You can also use three equal signs === to test if they are equal and of the same type. We’ll go into this more later.)

  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    The page should say a is equal to b.

  4. Let’s see what happens if we make it so they are not equal. Switch back to your code editor.

  5. Set $a to 20 as shown in bold:

    $a = 20;
    $b = 8;
    
  6. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    The page will be blank. Because $a is now larger than $b, the code between the {} does not execute at all.

  7. Switch back to your code editor.

  8. Now we can add an else statement. This will execute if the if statement is false. Add the following bold code:

    if ($a == $b) {
       echo "a is equal to b";
    }
    else {
       echo "a does not equal b";
    }
    
  9. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    Now the else statement will run and your page should say a does not equal b.

Elseif Statements

But what if we want to display a message if $a is GREATER than $b? We can use an elseif statement.

  1. Switch back to your code editor.

  2. In between the if and the else statements add an elseif as shown in bold:

    if ($a == $b) {
       echo "a is equal to b";
    }
    elseif ($a > $b) {
       echo "a is greater than b";
    }
    else {
       echo "a does not equal b";
    }
    
  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    Now the elseif statement will run and your page should say a is greater than b.

Switch Statements

Sometimes you need to execute different pieces of code depending on what a particular variable (or expression) is equal to. Using multiple elseif statements could get tedious and hard to read. That’s where the switch statement comes in.

For this example pretend the user is selecting different pages for navigation on a website. Depending on which one the user chooses, different code will execute.

  1. Switch back to your code editor.

  2. Delete everything between the <?php ?> tags, then enter the following bold code:

    $nav = "home";
    switch ($nav)
    {
       case "home":
          echo "Take me to the home page.";
          break;
       case "news":
          echo "Take me to the news section.";
          break;
       case "about":
          echo "Take me to the about page.";
          break;
    }
    

    Here’s what’s going on with this code:

    • First we set the variable $nav equal to “home”.
    • switch ($nav) indicates that different code will execute depending on what $nav is equal to.
    • case "home": if the $nav variable is equal to "home" then do the code in that section.
    • break; this tells PHP to stop executing the section. If you did not include the break commands PHP would run every case!
  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    The page should say Take me to the home page.

  4. Switch back to your code editor.

  5. Change the $nav variable to equal news:

    $nav = "news";
    
  6. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    The page should now say Take me to the news section.

  7. Switch back to your code editor.

  8. Try changing $nav to sports:

    $nav = "sports";
    
  9. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    The page will be blank. Let’s set a default value in case the user doesn’t choose anything.

  10. Switch back to your code editor.

  11. Enter the following bold code:

    switch ($nav)
    {
       case "home":
          echo "Take me to the home page.";
          break;
       case "news":
          echo "Take me to the news section.";
          break;
       case "about":
          echo "Take me to the about page.";
          break;
       default:
          echo "Please choose a page.";
          break;
    }
    
  12. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    It will now say Please choose a page.

More Comparison Operators

To test if something is not equal, we use the != sign.

  1. Switch back to your code editor.

  2. Delete everything between the <?php ?> tags, then enter the following bold code:

    $a = 3;
    $b = 4;
    
    if ($a != $b) echo "a does not equal b.";
    

    This tests to see if $a is equal to $b. You’ll also notice that we are using a shorthand of the if statement. If the statement is true, PHP will execute the code after the (). No need for brackets in this simple case!

  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    As predicted, $a does not equal $b.

Logical Operators

Let’s take a quick look at some different comparison operators such as AND and OR.

  1. Switch back to your code editor.

  2. Delete the if statement and replace it with the following bold code:

    $a = 3;
    $b = 4;
    
    if ($a == 3 || $b == 3) echo "One of these is 3.";
    

    This tests to see if either $a or $b is equal to 3.

  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    The page should say One of these is 3.

  4. Switch back to your code editor.

  5. Change the || to an &&, to test if they are BOTH equal to 3. Enter the following bold code:

    if ($a == 3 && $b == 3) echo "Both of these equal 3.";
    
  6. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    You’ll see that the code does not fire, because it is not true.

The Difference Between == & ===

Double equal signs will test for equality, but are a bit more loose about the data type that they test for. For example, if you have an integer set to 3, and a string set to the character ‘3’, the double equal sign would say that they are equal. However, if you have a triple equal sign, they would NOT be equal because they are different types (one is a numeric value, and the other is a string).

  1. Switch back to your code editor.

  2. Delete everything between the <?php ?> tags, then enter the following bold code:

    $a = 3;
    $b = '3';
    
    if ($a == $b) echo "a equals b.";
    if ($a === $b) echo "a is the same type as b.";
    
  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/ifelse.php
    • Windows: localhost/phpclass/ifelse.php

    You’ll see that only the first echo statement is run because the two variables are not of the same type.

  4. Close the file. We’re done with it.

Comparison Operators

Example Description
$a == $b True if $a equals $b
$a != $b True if $a does not equal $b
$a === $b True if $a equals $b and is of the same type
$a !== $b True if $a does not equal $b or they are not the same type
$a > $b True if $a is greater than $b
$a < $b True if $a is less than $b
$a <> $b True if $a does not equal $b
$a <= $b True if $a is less than or equal to $b
$a >= $b True if $a is greater than or equal to $b

Logical Operators

Example Description
$a and $b True if $a and $b are both true
$a or $b True if $a or $b is true
$a xor $b True if $a or $b is true, but not if both are true
!$a True if $a is not true
$a && $b True if $a and $b are both true
$a || $b True if $a or $b is true

How to Learn Full-Stack Web Development

Master full-stack web development with hands-on training. Build fully functional websites and applications using HTML, CSS, JavaScript, Python, and web developer tools.

Yelp Facebook LinkedIn YouTube Twitter Instagram