PHP Logical Operators

5 min read

It's often not enough to check only one condition in an if-statement. For example, if you do a password query, you want to know if the username and password are correct.

For this purpose, there are logical operators. Through any number of conditions can be checked. If you want to test several conditions, the following keywords exist:

  • Both conditions must be fulfilled by using AND or alternative notation &&.
  • By using OR or the alternative notation ||, only one condition has to be fulfilled.
  • By the exclamation mark !, If conditions can be negated, that is, if they are met, the condition must not apply.
  • By using brackets ( and ), conditions can still be grouped.

AND and OR operators

If two or more conditions are linked by AND, all conditions must be met. The alternative keyword is &&. Which one chooses, does not matter, and is up to the personal taste.

With OR, it is sufficient if one of the conditions is already fulfilled. The alternative notation is | |, Below is an example

<?php $username = "Wira";
$password = "php-easy";
if ($username == "Wira" AND $password == "php-easy") {
echo "Both conditions were met - access allowed. <br />";
} if ($username == "Wira" OR $password == "php-simple") {
echo "One or both conditions were met.";
}
?>

For the first if statement, $username and $password are checked together. Only if both entries are correct, the corresponding text is output. For the second if statement, it is sufficient if a condition is met, i.e., the $username or the $password are correct.

AND and OR allow more than two conditions to be checked:

<?php $salary = 25000;
$fortune = 10000;
$inheritance = 0;
if ($salary > 10000 OR $fortune> 50000 OR $inheritance> 1000000){
echo "Wow, you're rich!";
} else if ($salary <1000 AND $fortune <10000 AND $inheritance == 0) {
echo "Small salary, little wealth, no inheritance, too bad!";
} else {
echo "None of the conditions was met";
}
?>

Conditions Group using parentheses

Sometimes one would like to combine AND and OR operators together to allow specific alternatives. For example a password query, you want to allow different users. To make the coherence of the operators clear, they can be grouped using parentheses.

<?php
$username = "Wira";
$password = "php-easy";
if (($username == "Wira" AND $password == "php-easy") OR ($username == "Tama" AND $password == "secret")) {
echo "Username and password matched. <br /> ";
}
if ($username == "Wira" AND ($password == "php-easy" OR $password == "secret")) {
echo "The username was Wira, and the password was either php-simple or secret." ;
}

The first if-statement is only satisfied if the user Wira with the password php-simple or the user Tama and the password was secret.

For the second if statement, the username must be Wira, and the password must be either php-simple or secret for the if statement to be executed.

Negation of conditions

By using the exclamation mark (!), such a condition can be negated. The first example you had seen in the article if statements. Whether a variable is different from another variable can be checked by $variable1 != $variable2. We can also negate one condition or several conditions by writing the exclamation mark at the beginning of it.

<?php
$number = 25;
if ($number >= 10 AND $number <= 20) {
echo "The number is between 10 and 20. <br />";
}
if (!($number >= 10 AND $number <= 20)) {
echo "The number was NOT between 10 and 20 <br />";
}
?>

In the second if statement, the condition is denied, that is, it is satisfied that the number is not between 10 and 20. In such a case, the number was either less than 10 or greater than 20.

Exercise: Transform the second if-statement so that no negation (!) but only AND, OR and corresponding comparison operations like <, <=, etc.

Example: Password query

For a real password query, one would enter the variables $username and $password via form input.

For this purpose, we first need a form with two input fields.

<form  action  =  "page2.php"  method = "post" >
Username: <br>
<input type = "Text" name = "username" /> <br />
Password: <br />
<input type = "Password" name = "password" /> <br />
<input type = "submit" value = "Submit" /> <br />
</form>

In this form, you enter the username and the password. If you then click Submit, page2.php is called and passed to both inputs.

page2.php must look like this:

<?php
$username = $_POST ["username"];
$password = $_POST ["password"];
if ($username == "Wira" AND $password == "php-easy") {
echo "Access allowed"; }
else {
echo "access denied!";
}
?>

First, in the above script, the data of the form is queried using $_POST and stored in the corresponding variables. If you use our interactive editor, press $ to edit the values ​​of the two POST variables.

An if statement and the logical operator AND are used to check whether $username has the contents Wira and $password has the content php-simple. Only if both inputs are correct, the message access allowed is displayed. Instead of this simple message, you could also hide a secret, internal page there. And you already have the password protection on your side.

« »