DevPush

How to use if statements in PHP

Introduction

This guide is especially for newbies and beginners who are new to programming as the explanations used here are for people who aren’t at the technical level of a programmer just yet.

One of the most fundamental things you need to learn are if statements as they allow you to run code for specific conditions.

I recommend using the online PHP editor onlinephp.io to run the code examples included in this guide.

onlinephp-code.png

How If statements work

It’s a way to run code selectively, so the condition needs to be true for the code within the if statement to be run.

Here is an image showing code in a descriptive form to make the explanation of how they work easier.

php-if-statement-explanation-2.png

How to use

This section will go through the different ways you can use if statements, the code examples below will be based on an online shop just like Amazon.

Checking true/false (booleans)

A boolean value can be either true or false when adding the boolean value within the if statement the value must be true for the code within the brackets to run.

<?php

if (true) {
    echo 'If statement was true' . PHP_EOL;
}
echo 'Code run';

The above example will print out both lines, you can use the online PHP editor to test just code.

php-if-statement-true.png

The next example will check if the condition is false, this means the code within the brackets won’t be run so only one line will be printed out.

<?php

if (false) {
    echo 'If statement was true' . PHP_EOL;
}
echo 'Code run';

The false code example is shown running below.

php-if-statement-false.png

Typically when using if statements you would check PHP variables rather than adding booleans directly within the if condition.

The statement in the if statement is true as the variable is set to true.

<?php

$discount = true;

if ($discount == true) {
    echo 'Discount was applied' . PHP_EOL;
}
echo 'Checked for discount code';

Here is the example when the variable is set to false.

<?php

$discount = false;

if ($discount == true) {
    echo 'Discount was applied' . PHP_EOL;
}
echo 'Checked for discount code';

Checking text (strings)

Not only can booleans be checked but also strings can too.

The example below checks that the variable string exactly matches the condition.

<?php

$cartStatus = 'empty';

if ($cartStatus == 'empty') {
    echo 'Shopping cart is empty';
}
echo 'Checked shopping cart';

Here is an example of the if statement being false.

<?php

$cartStatus = 'filled';

if ($cartStatus == 'empty') {
    echo 'Shopping cart is empty';
}
echo 'Checked shopping cart';

Checking numbers (integers)

Numbers can be checked as well.

The variable with the total number of items in a cart which is 10 is checked against the value 10, this equates to true.

<?php

$totalCartItems = 10;

if ($totalCartItems >= 10) {
    echo 'Shopping cart is full';
}
echo 'Checked shopping cart';

And when the variable is set to 9 the statement is false.

<?php

$totalCartItems = 9;

if ($totalCartItems >= 10) {
    echo 'Shopping cart is full';
}
echo 'Checked shopping cart';

Using else

You can use the else statement to run code that doesn’t equate to true inside of the if statement.

The code below will run within the ”else” part.

<?php

$discount = false;

if ($discount == true) {
    echo 'Discount was applied';
} else {
    echo 'Discount was not applied';
}

You can have multiple if statement checks by adding an extra else if statement.

$cartStatus = 'filled';

if ($cartStatus == 'full') {
    echo 'Shopping cart is full';
} else if ($cartStatus == 'filled') {
    echo 'Shopping cart is filled';
} else {
    echo 'Shopping cart is empty';
}

Here is an example using strings.

$cartStatus = 'empty';

if ($cartStatus == 'full') {
    echo 'Shopping cart is full';
} else if ($cartStatus == 'filled') {
    echo 'Shopping cart is filled';
} else {
    echo 'Shopping cart is empty';
}

Checking for false

You can change the statements so that rather than checking for true the condition can be checked for false as well.

<?php

$discount = false;

if ($discount == false) {
    echo 'Discount was not applied' . PHP_EOL;
}
echo 'Checked for discount code';

And here is an example checking if the string is not equal to the value “empty”.

<?php

$cartStatus = 'filled';

if ($cartStatus != 'empty') {
    echo 'Shopping cart is not empty';
}
echo 'Checked shopping cart';

Conclusion

After reaching this part you will have learned the following.

  • With booleans
  • With strings
  • With integers
  • Using the ”else” and “else if” statements
  • Checking for “false”

Hope you were able to follow this guide easily and more helpful PHP guides will be posted in the next few weeks or so.