From the course: PHP for WordPress

Comparison operators and the truth - PHP Tutorial

From the course: PHP for WordPress

Start my 1-month free trial

Comparison operators and the truth

- [Instructor] Determining if something is true or false is a powerful aspect of a programming language. It's the root of all decision making. And it's how you tell the program exactly what to do and when to do it. So, let's take our first step into decision making in programs and look at comparison operators. Comparison operators help us compare values or variables. And we will learn how they work and how to combine those operations to check for complex conditions. First, a comparison operator allows us to compare values and evaluate them as true or false. The most basic comparison operator is the equality operator, which is two equal signs. And it's two equal signs because a single equal sign is reserved for assignments. So, if you want to check to see if two values are equal, you would say something like 10 equals equals 10 and that of course is true. 10 equals equals 20 is false. And since PHP is weak typed, checking for equality only ignores the type of variable. PHP will do the conversion for us. So, the character one, which you see in quotes here, equal equal the integer one would evaluate to true. PHP would convert them both to the same type, and then compare them. If you do want to check for both type and value, which you might want to in some cases. You can use the identical comparison with three equal signs. So, one equal equal equal one if they're both integers is true, but the character one equal equal equal one is false because they're not the same type. This will come in handy if you want to check to see if a value is zero because zero is often represented as false in PHP. So, if you want to say is something zero and I want this to be a true statement. You will check for the identical comparison. There are also a number of other comparisons we can make with PHP. We can do greater than with the greater than sign or the right angle bracket. And that is 10. Greater than 10 is false. 10 greater than five is true. The same thing with less than and the left angle bracket. So, 10 less than 10 is false. 10 less than five is also false. We can do greater than or equal to with the right angle bracket combined with the equal sign. And similarly we can do less than or equal to with the left angle bracket and the equal sign. Finally, we can negate a statement. This is called the NOT operator and it's represented by an exclamation point. So, exclamation point, equal sign is not equal. And exclamation point equal equal is not identical. So in this case, 10, exclamation point, equal 10 would be 10 does not equal 10, which is false. We can also combine the NOT operator with parentheses. So, we can take the statement 10 equal equal 10, which is true 10 equals 10. And if we put that in parentheses and proceed it with a NOT operator, that would evaluate to false because we're essentially saying the opposite of 10 equals 10. These are often used as Boolean statements or Boolean operators because they always evaluate to true or false. And though we've only looked at numbers and basic examples in PHP programs. You can also use Boolean operators or Boolean values to check the state of a website. You saw that earlier, when we check to see if a user was logged in that either returned true or false. And we'll look at that even more later. These are going to come in handy once you start learning about control structures and loops, but for now, there's one more type of operator that you should know about.

Contents