Press enter to see results or esc to cancel.

PHP Lesson 11: Logical operators AND, OR, !, &&, || ()

Share this
PHP Lesson 11: Logical operators AND, OR, !, &&, || () - PHP Lesson 11: Logical operators AND, OR, !, &&, || () - PHP Lesson 11: Logical operators AND, OR, !, &&, || () - php logical operators
php logical operators

Often it is 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 the logical operators. By means of any number of conditions can be checked. To check several conditions, the following keywords exist :

  •   Both conditions must be fulfilled by means of AND  or alternative notation &&.
  • 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.
  • Using brackets (   and  )  , conditions can still be grouped.

PHP AND and OR operators example

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

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

$username = "coding180";
$password = "pass";
if ($username == "coding180" AND $password == "pass") {
   echo "Both conditions were met - access allowed. <br />";

if ($username == "coding180" OR $password == "pass") {
  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:

$salary = 25,000;
$fortune= $10000;
$inheritance = 0;

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

Logical Conditions Group using parentheses – PHP

Sometimes one would like to combine AND and OR operators together to allow certain alternatives. For a password query, for example, you want to allow different users. In order to clarify the coherence of the operators, they can be grouped by means of parentheses.

$username = "coding180";
$password = "pass";

if (($username == "coding180" AND $password == "pass") OR ($username == "Paul" AND $password == "secret"))
  echo "username and password matched. <br/>";

if ($username == "coding180" AND ($ password == "pass" OR $password == "secret")) {
  echo "The username was coding180, and the password was either pss or secret.";

The first if-statement is only satisfied if the user coding180 with the password pass or the user Paul and the password was secret.

For the second if statement, the username must be  coding180, and the password must be either  pass or  secret for the if statement to be executed.

Negation of conditions – PHP

By the exclamation mark   conditions can be negated. The first example you had seen in the article to 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.

$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 with ! is used, but only AND, OR and corresponding comparison operations such as <, <= etc.


Example: Password query – PHP

For a real password query, you would enter the variables $username and $password by form.

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

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

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

page2.php must look like this:

$username = $ _POST["username"];
$password = $ _POST["password"];

if ($username == "coding180" AND $password == "pass")
   echo "access allowed";
   echo "access failed";

First, in the above script, the data of the form is queried using $ _POST and stored in the corresponding variables.

An if statement and the logical operator AND are used to check whether $username has the content coding180 and $password has the content pass. 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 a password protection on your side.

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this