(click anywhere to close)
OPEN MENU

[PHP] Conditionals

category: Website | course: PHP | difficulty:

After all this talk about floats, integers, and strings, you might start to think – Booleans can only be true or false, how important could they possibly be? Well, extremely important. And that’s an understatement.

To computers, the world is binary and exact. Computers can’t “sorta execute this”, or “just try to do this or that”. They either do it, or they don’t. Something is either executed exactly as it was written, or it isn’t executed at all. Therefore, we must use Booleans to tell the computer when to execute which pieces of code.

For example, we could check whether a user is logged in. If the check returns true, we can execute a piece of code that welcomes him and throws confetti into his face. If the check returns false, we display a login page. A user can’t be “sort of logged in” – he either is, or he isn’t.

Similarly, we could check whether a search query was successful, by checking the amount of results it returned. We could write a piece of code that checks whether or not that amount is greater than 0. If the check returns true, we have results and we should display them. If the check returns false, we have no results and we should suggest a different query to the user. A query can’t have more than zero results, yet zero results – either there are results, or there aren’t.

Not surprisingly, we have another set of operators at our disposable for this important aspect of PHP; the comparison operators and logical operators. Both of these are conditional operators, and using them creates a conditional – a certain condition under which the program should or shouldn’t do something.

Comparison Operators

In our examples above, we already saw how we could compare two values to get a truth value. As it turns out, comparison is the best and only way to do so; is this greater than that? Or is this less than that? Or is this equal to that?

Therefore, if you place any of the following comparison operators between two operands, PHP will not perform some arithmetic operation – instead, it will check whether the comparison is true or false, and return that.

Operator

Name

Description

==

Equal

Returns true if both operands are equal (after type conversion)

===

Identical

Returns true if both operands are equal, and of the same data type

!=
<>

Not Equal

Returns true if both operands are not equal (after type conversion)

!==

Not Identical

Returns true if both operands are not equal, or not of the same data type

<

Less than

Returns true if the left hand side is less than the right hand side

>

Greater than

Returns true if the left hand side is greater than the right hand side

<=

Less than or equal to

Returns true if the left hand side is less than or equal to the right hand side

>=

Greater than or equal to

Returns true if the left hand side is greater than or equal to the right hand side

//This is true, because "2" and 2 have the same integer value
$cond = (2 == "2");

//This is false, because "2" and 2 are not identical (one is integer, one is string)
$cond = (2 === "2");

//This is true, because "Panda" is equal to "Panda"
$cond = ("Panda" <= "Panda");

//This is false, because true is the same as true
$cond = (true != true);

Logical Operators

Performing one comparison is great, but it’s usually not enough. For example, if a user logs in we want to check whether the username matches the password. In other words, only if our check for the username returns true, and our check for the password returns true as well, we can log the user in. To combine conditionals, we can use the following logical operators:

Operator

Name

Description

and
&&

Logical AND

Returns true if both operands are true

or
||

Logical OR

Returns true if either the left or the right operand is true, or both

xor

Logical XOR

Returns true if either the left or the right operand is true, but not both

!

Logical NOT

Returns true if the operand is not true. (It negates the value.)

$password = "1234";
$username = "Ginty";

//This is true, because both password and username are correct
$cond = ($password === "1234" && $username === "Ginty");

//This is false, because both are true, so the inversion is false
$cond = (!($password === "1234" && $username === "Ginty"));

//This is not secure; it already returns true if only one of the two is correct!
$cond = ($password === "1234" || $username === "Ginty");

//If a user has at least 10 credits, or it is a new user, return true
//(so that we can level up the user, or give him bonus credits, or whatever)
$cond = ($credits_total >= 10 || $new_user);

Even though the function of and and && is exactly the same, just as or and ||, there’s a difference in precedence. The shorthand operators && and || have a higher precedence than and and or, which means they are evaluated first.

CONTINUE WITH THIS COURSE
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)