Programming in PHP - Logical Operators
Posted 01/30/2015 at 16:04 by manulaiko3.0
Copy and paste from my website: 
In the last chapter we’ve seen maths, now we’re going to talk about logic.
Logic operators takes two booleans and returns another boolean (or takes two integers and returns one boolean, or takes two integers and returns anther integer :S)
Without talking more shit we’re going to touch logic operators right now!
And
Some times you will need to compare two things:
So, how is this translated to PHP?
<?php $red = true; $big = false; $car = $red && $big; ?> This code will return false, why?
Because you want a fucking big and red car, not a red car. To make the variable $car true we have to modify the code to this:
<?php $red = true; $big = true; $car = $red && $big; ?> Now yes, it’s tre, you’ll buy it!!
Here’s a table so you can see the combinations:
Operator 1 Operator 2 Result true true true false true false true false false false false false OR
You’ve realized that the big and red car is too expensive for your pocket, what you can do?
You can get more money (drugs) or rethink what you want (a part from drugs):
<?php $red = true; $big = false; $car = $red || $big; //true! ?> So, the or (||) operator returns true if any of the operator is true.
Here’s a table so you can see the combinations:
Operator 1 Operator 2 Result true true true false true true true false true false false false Not
Now you don’t want a red car, you want a green car (peace on you):
<?php $red = true; $car = !$red; //false ?> Bassically, the not (!) operator inverts the value given.
Here’s a table so you can see the combinations:
Operator 1 Result true false false true And it’s done, now you know what are logical operators, the next chapter I’ll talk about Comparation Operators
See you!

In the last chapter we’ve seen maths, now we’re going to talk about logic.
Logic operators takes two booleans and returns another boolean (or takes two integers and returns one boolean, or takes two integers and returns anther integer :S)
Without talking more shit we’re going to touch logic operators right now!
And
Some times you will need to compare two things:
I want a red AND big carIf the car is red and big you’ll buy it, but if the car is red and small or green and big you won’t buy it, you want a fucking big red car!
So, how is this translated to PHP?
<?php $red = true; $big = false; $car = $red && $big; ?> This code will return false, why?
Because you want a fucking big and red car, not a red car. To make the variable $car true we have to modify the code to this:
<?php $red = true; $big = true; $car = $red && $big; ?> Now yes, it’s tre, you’ll buy it!!
Here’s a table so you can see the combinations:
Operator 1 Operator 2 Result true true true false true false true false false false false false OR
You’ve realized that the big and red car is too expensive for your pocket, what you can do?
You can get more money (drugs) or rethink what you want (a part from drugs):
I want a red OR big carThe red and big car is really expensive, but a red car or a big car isn’t so expensive. Let’s see how to implement this on PHP:
<?php $red = true; $big = false; $car = $red || $big; //true! ?> So, the or (||) operator returns true if any of the operator is true.
Here’s a table so you can see the combinations:
Operator 1 Operator 2 Result true true true false true true true false true false false false Not
Now you don’t want a red car, you want a green car (peace on you):
I want a not red car, I don’t give a fuck if it’s big or smallAnd here’s the code in PHP:
<?php $red = true; $car = !$red; //false ?> Bassically, the not (!) operator inverts the value given.
Here’s a table so you can see the combinations:
Operator 1 Result true false false true And it’s done, now you know what are logical operators, the next chapter I’ll talk about Comparation Operators
See you!
Total Comments 0






