PHP Logical operators

Register here to save your progress

? ? ? ? ?

Intro

Logical operators are used to check multiple conditions.

For example we want to check in the same time if a variable is bigger than 5 and also to check if another variable is not empty. If the conditions are meet than the operator will return true, else it will return false.

Values that represent false are:

  • the FALSE value itself;
  • the numeric value 0;
  • empty strings and the strings with the value '0';
  • arrays with zero elements;
  • the NULL value;

As long as your variable doesn't has those values it's true;

The logical operators are: 

Name Operator Example Result
And &&
same as
and
$a && $b
$a and $b
If both $a and $b are true it will return true, else it will return false
Or ||
same as
or
$a || $b
$a or $b
If both $a and $b are true it will return true, also if either $a or $b is true it will return true, else it will return false
Xor xor $a xor $b If either $a or $b is true, but not both in the same time, it will return true, else it will return false
Not ! !$a If $a is true it will return false, if $a is false it will return true

What type of value is a logical operator returning?

&& (and) Operator

The And operator has the symbol &&, it can also be used and but the most common way is '&&'.

This operator is useful when we want to be sure that all our conditions are met, since every conditions needs to be true. 

For example, in a registration form we want to be sure that the first name, last name and email are all not empty.

<?php

$first_name = 'Alice';

$last_name = 'Wonderland';

$email = '';

var_dump($first_name && $last_name && $email);
//will output false, since the variable $email is empty


$email = 'alice@wonderland.com';

var_dump($first_name && $last_name && $email);
//will output true


$pet_price = 20;
$pet_name  = 'White Rabbit';

//check the pet name 
//and if the price is bigger than 5
var_dump($pet_price > 5 && $pet_name === 'White Rabbit');
//will return true

$place_name = 'Wonderland';
$place_location = null;

//check if we have values for the variables
var_dump($place_name && $place_location);
//will output false, since NULL means false to our operator


?>

What will return the following statement?

<?php

var_dump('And quiz' && 0 === false);

?>

|| (Or) Operator

The Or operator is using the symbol ||, also there is the or symbol, but most commonly  is used '||'.

In order for this operator to return true at least one of the conditions must be true.

As an practical example we may want to check if a user has entered as a contact method either his email or phone number.

<?php

$email = 'mike@redtutorial.com';

$phone_number = '';

$address = '';

var_dump($phone_number || $email || $address);
/* 
will output true since the email is not empty, 
even though the $phone_number and the $address are empty
*/

$price = 125;
$promo_code = 'DISCOUNT';

var_dump($price > 120 || $promo_code === 'DISCOUNT');
/* 
will output true, even though at least one condition
should've been true both are true
*/

$payment_method = 'cash';

var_dump($payment_method === 'credit_card' || $payment_method === 'paypal');
/*
will output false since not even at least one condition 
was true
*/

?>

Which of the following scripts will return true?

xor Operator

xor operator is similar to the or ( || ) operator but this time only one option needs to be true. In practice this operator is not so commonly used by developers as and, or and not operators. 

One practical example would be for example when having two inputs, lets say we have an article two write and there is a input for setting the article public and another one for setting it draft, and we need to choose only option.

<?php

$article_is_public = 1;
$article_is_draft  = 1;

var_dump($article_is_public xor $article_is_draft);
//will output false since both variables are true ( 1 equals with true when evaluated)

$article_is_draft = 0;

var_dump($article_is_public xor $article_is_draft);
//will output true since only article_is_public variable is true now

Which of the following statements are true?

! (Not) Operator

The Not operator is represented by the symbol !. This operator has the role to invert the logical value of the statement, more simply said if a variables value is true than it will return false, and if the value is false it will return true. 

For example we have a variable and we want to get true if the variable's value is an empty string, in this case we can simply put this operator before it.

<?php

$name = '';

var_dump(!$name);
//will output true because the value of $name is equal to false, but we the operator inverted it

$account_activated = true;

var_dump(!$account_activated);
//will output false, the inverted value

$user_email = 'robert@redtutorial.com';
$user_phone_number = '';

var_dump( !($user_email || $user_phone_number) );
//will return false since the inner statement is evaluated to true, but the ! operator inverted it

Which of following statements will return the logical value true?