Programming in PHP - Comparation Operators
Posted 01/30/2015 at 16:07 by manulaiko3.0
Copy and paste from my website: 
So, we’ve already seen Math Operators to do shit maths and Logical Operators, to choose something. Now we want to compare two things, how will we do it? With Comparations Operators (I don’t know english name so I’m sure it’s 100% wrong name).
There are 6 types of operators that can be used to compare things:
Look at this code:
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; //Script ends here, it's not an error, PHP scrips doesn't needs "?>" at the end //of the script if there's nothing else in the file We want to check the value of $variable3 is 0, for that we will use == operator.
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 == 0); //True because 10 (variable1) - 10 (variable2) = 0 Not Equal
Code:
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; We want to check the value of $variable3 is not 0, for that we will use != operator.
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 != 0); //False because 10 (variable1) - 10 (variable2) = 0 Less
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is less than 5000, we will use the operator <:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 < 5000); More
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is more than 5000, we will use the operator >:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 > 5000); Less or Equal
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is less or equal than 5000, we will use the operator <=:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 <= 5000); More or Equal
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is more or equal than 5000, we will use the operator >=:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 >= 5000); And that’s all, you might noticed that the last 4 examples are copy & paste, well, I’m sorry but the thing is the same and I’ve just came home from school so I’m tired.
Nex chapter: Binary Operators (Yes, it will be boring, but you can skip it, it’s not used in not complex projects)
See you!

So, we’ve already seen Math Operators to do shit maths and Logical Operators, to choose something. Now we want to compare two things, how will we do it? With Comparations Operators (I don’t know english name so I’m sure it’s 100% wrong name).
There are 6 types of operators that can be used to compare things:
- Equal
- Not equal
- Less
- More
- Less or Equal
- More or Equal
Look at this code:
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; //Script ends here, it's not an error, PHP scrips doesn't needs "?>" at the end //of the script if there's nothing else in the file We want to check the value of $variable3 is 0, for that we will use == operator.
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 == 0); //True because 10 (variable1) - 10 (variable2) = 0 Not Equal
Code:
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; We want to check the value of $variable3 is not 0, for that we will use != operator.
<?php $variable1 = 10; $variable2 = 10; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 != 0); //False because 10 (variable1) - 10 (variable2) = 0 Less
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is less than 5000, we will use the operator <:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 < 5000); More
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is more than 5000, we will use the operator >:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 > 5000); Less or Equal
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is less or equal than 5000, we will use the operator <=:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 <= 5000); More or Equal
The code:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; Now we want to see if $variable3 is more or equal than 5000, we will use the operator >=:
<?php $variable1 = 5321; $variable2 = 10351; $variable3 = $variable1 - $variable2; $booleanResult = ($variable3 >= 5000); And that’s all, you might noticed that the last 4 examples are copy & paste, well, I’m sorry but the thing is the same and I’ve just came home from school so I’m tired.
Nex chapter: Binary Operators (Yes, it will be boring, but you can skip it, it’s not used in not complex projects)
See you!
Total Comments 0






