Tutorial 15: Logical Operators


Logical Operators are just that: logical! It's like speaking english. They are very helpful when you need your If / Else / Elseif statements to be a little more complex. Check out this table, provided by PHP.net:

Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.

Here is an example of how you would use one of the above logical operators:

$username = "johnnyboy";
$password = "qwerty";

if ($username == 'johnnyboy' && $password == 'qwerty') {
	
	// username and password are correct
	
} else {

	echo "Your username and password combination are incorrect";

}

Feel free to experiment with all of the above logical operators with some PHP If / Else / Elseif statements using the practice.php page provided in this folder.

Check out the final example
©2019 Brad Hussey