Operators are used to perform various operations on variables and values.
In this lesson we will do an overview of several operators. It is important to note that PHP divides operators into the following Groups:
As a result of the amount of operators that can be found across these groups, we will go over a few examples in each group. The file used in this lesson is located in your downloads folder. You can use it for future reference, and also go through each operator if you require further information.
We will start with arithmetic operators.
The first section in our PHP file, provides an example of the different arithmetic operators available, including addition, subtraction, multiplication, division and modulus.
We can see the output of each echo statement in our web browser.
An assignment operator is used to set the value of the left operand to the expression on the right, in an equation.
For example, in the equation $x=20. We have assigned the value 20 to x. The output would result in the number 20.
Comparison Operators are used to compare two values (numbers or strings).
In the first example, we have set the value of x to 300 and y to 300 with quotes.
We use the var_dump function to compare x to y using a double equals sign (==).
The output is true because the value of x is equal to y.
The next operator, (===) is used to see if the left operand is identical to the right. In this case, 300 with no quotes is not identical to 300 without quotes. For that reason false is outputted.
You can go through the remainder of the comparison operators to see the result. They are fairly straight forward, and the comments explain what each one does.
Increment operators are used to incremement or decrement the value of a variable.
We can see in the first example, the initial value of x is set to 50. Using the x++; incremement operator, we can increase the value of x by 1.
We can also decrease the value using a decrement operator, such as the last third example, which decrements 50 by 1 to output 49.
Logical operators are used to combine conditional statements.
In the first example, we have two variables x and y. x is set to 200 and y to 300.
We use an and operator, to check if x is equal to 200 and y is equal to 300. We instruct the script to output the word true, if both those conditons are met.
There are a variety of other logical operators we can use, but we will cover most of them throughout different sections in this course. You can go over the examples for a better understanding of each.
Next we have string operators. These are specially to be used with strings.
The first one allows us to attach one string variable to another. In this case our web browser will output Hello World on the same line, because we have concatenated variable x and y.
Array operators allow us to compare and manipulate arrays.
In the first example we create a union of two arrays, (x and y) by using the plus (+) operator.
In the second example, we check to see if array x is equal to array y.