Website Creation with PHP Operators
Operators are special characters or a collection of characters that have a special meaning. That's vague and doesn't really help you understand what they are, but they are difficult to explain until you realize that you already know several operators from your math classes. PHP has 6 groups of operators, assignment, arithmetic, comparison, logical, array, and incrementing/decrementing. For people learning to create a website in PHP, you really only need to care about assignment, arithmetic, logical, and comparison for now.
Let's start with arithmetic operators as you likely already know them. We have addition, subtraction, multiplication, division, and a modulus. If you don't know what modulus is, don't worry, it's quite uncommon when first starting out creating a website and you might not need to know what it does for a few years. Back to the main arithmetic operators, they are probably exactly what you would expect. Take a look at the following code to see how they work.
1 + 1; //equals 2
2 - 1; //equals 1
2 * 2; //equals 4
4 / 2; //equals 2
7 % 3; //equals 3
Yep, those are crazy simple and you should already know them (except the last one). If not, I worry for you and you probably ought to brush off the old math books. Up next is the assignment operator. Like the previous operators, it is crazy simple and you should already understand it. I won't cover other assignment operators as you will learn them later. The assignment operator is literally just the equal sign. This is how you set a variable (described in the next lesson) to a particular value just like you learned, hopefully, in algebra.
$a = 1; //$a is a variable that now has the value of 1
Next, we will discuss the comparison operators. They are extremely valuable at, you guessed it, comparing things. When comparing variables, you can say things like “when x is less than y” or alternatively “when x is greater than y”. We can also include comparisons like is equal to, is greater than or equal to, is less than or equal to. Those are the most common comparison operators you will use when creating a website. Let's take a look at some code to see them in action.
1 > 2; //false, it's asking if 1 is great than 2
1 < 2; //true, it's asking if 1 is less than 2
2 <= 2; //true, it's asking if 2 is less than or equal to 2
2 >= 2; //true, it's asking if 2 is greater than or equal to 2
1 == 1; //true, it's asking if 1 is equal to 1
1 != 2; //true, it's asking if 1 is NOT equal to 2
Last but not least, we will see what logical operators can do and why they are important to help your code be more concise when creating a website. Put simply, you really only need to care about two logical operators as they are the most frequently used. The first is && that basically says the condition before and this condition is true. The next logical operator is just || that means or as in if the first condition or this one is true. Let's see how they look in the following example.
1 == 1 && 1 == 2; //false, while the first condition is true, the second isn't because one doesn't equal two
1 == 1 && 2 == 2; //true, while the first condition and second condition are both true
1 == 1 || 1 == 2; //true, the first condition is true, so it doesn't matter if the second condition is actually false
1 == 2 || 2 == 3; //false, both the first and second condition aren't true