PHP Operators

An operator is a symbol that manipulates one or more values, usually producing a new value in the process. Meanwhile, an expression in PHP is anything that evaluates to a value; this can be any combination of values, variables, operators, and functions. In the preceding example, $x + $y is an expression. Here are some more examples of expressions:
$x + $y + $z
$x -$y
$x 5
gettype( $test_var ) The values and variables that are used with an operator are known as operands .
Operator Types
Operators in PHP can be grouped into ten types, as follows:
[table “” not found /]
The Assignment Operator
You have met the assignment operator each time we have initialized a variable. It consists of the single character =. The assignment operator takes the value of its right-hand operand and assigns it to its left-hand operand:
$name =”matt”;
The variable $name now contains the string “matt”. Interestingly, this construct is an expression. It might look at first glance that the assignment operator simply changes the variable $name without producing a value, but in fact, a statement that uses the assignment operator always resolves to a copy of the value of the right operand. Thus                print ( $name = “matt” );
prints the string “matt” to the browser in addition to assigning “matt” to $name.
For example,
$x = 4;
$x += 4; // $x now equals 8 is equivalent to $x = 4;
$x = $x + 4; // $x now equals 8
There is an assignment operator for each of the arithmetic operators and one for the concatenation operator.
[table “” not found /]
Arithmetic Operators
The arithmetic operators do exactly what you would expect. The addition operator adds the right operand to the left operand. The subtraction operator subtracts the right-hand operand from the left. The division operator divides the left-hand operand by the right. The multiplication operator multiplies the left-hand operand by the right. The modulus operator returns the remainder of the left operand divided by the right.
[table “” not found /]
The Concatenation Operator
The concatenation operator is a single dot. Treating both operands as strings, it appends the right-hand operand to the left. So

"hello"." world"

“hello world”
Regardless of the data types of the operands, they are treated as strings, and the result always is a string.
Comparison Operators
Comparison operators perform tests on their operands. They return the boolean value true if the test is successful, or false otherwise. This type of expression is useful in control structures, such as if and while statements.
To test whether the value contained in $x is smaller than 5, for example, you would use the less than operator:

$x < 5

If $x contained 3, this expression would be equivalent to the value true. If $x contained 7, the expression would resolve to false.
[table “” not found /]
These operators are most commonly used with integers or doubles, although the equivalence operator is also used to compare strings.

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.

Recommended Tutorials for you

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.