In this PHP tutorial we will learn about Comparison Operators.
Comparison Operators can compare 2 values, 2 strings or 2 variables that can contain any of them and will act upon that.
For this I am going to use an IF statement.
Lets start by creating the IF statement structure.
My condition is if 1==1.
echo.
True.
and then else.
echo.
False. Remember I don't need these brackets so I'm going to take them out.
Lets indent it
Never mind the indenting.
This is the first comparison operator.
Two = to means comparison operator. We've seen this in the IF statement before.
1 does equal to 1 so this will echo True. Lets try it.
We got True.
Let me change this. IF 1 is greater than 1 then lets see what result we get.
False, because 1 is equal to 1 and not greater than 1.
Now lets change this to 1 greater than or equal to 1
IF 1 greater than or equal to 1, echo True else echo False.
Here we should get True.
You can also do the same with less than or equal to. So for example less than
would be False, less than or equal to would be True.
We can also say not equal. So if 1 is not equal to 1 echo True
Refresh. We'll get False here because 1 is equal to 1. Now lets say if 1 isn't equal to 2
We get True because 1 is not equal to 2
These are the basic Comparison Operators that you will be using for our tutorials
Expand on this - practice them - and you'll understand them better
You can also compare variables using these operators. So for example num1 = 1
num2 = 2. All we now do is replace these values and there we go
This will produce exactly the same result as we've got earlier - which is True. Now all we need to do is change these values
Please note this will now read as num1 = 1 num2 = 1 so if num1 doesn't equal 1 it is False because 1 does equal 1 therefore we get False
These here are the simple Comparison Operators. Play around with them. See what you can do. Thanks for watching.
This is Mad Madhur dubbing for the Spoken Tutorial Project.