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 caption=”” width=”750″ colwidth=”250|500|50″ colalign=”left|left] Type, Description
Arithmetic ,Perform common arithmetical operations such as addition and subtraction
Assignment, Assign values to variables
Bitwise ,Perform operations on individual bits in an integer
Comparison, Compare values in a Boolean fashion ( true or false is returned)
Error, Control Affect error handling
Execution, Cause execution of commands as though they were shell commands
Incrementing/Decrementing, Increment or decrement a variable ’ s value
Logical ,Boolean operators such as and  or  and not that can be used to include or exclude
String, Concatenates (joins together) strings (there ’ s only one string operator)
Array ,Perform operations on arrays
[/table] 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 caption=”Assignment Operators ” width=”720″ colwidth=”220|250|250″ colalign=”left|left|left”] Operator, Example ,Equivalent to
+=, $x += 5, $x = $x + 5
− =, $x − = 5, $x = $x − 5
/=, $x /= 5 ,$x = $x / 5
*=, $x *= 5, $x = $x *5
%=, $x%=5, $x = $x % 5
.=, $x .= “test” ,$x = $x” test”
[/table] 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 caption=”Arithmetic Operators ” width=”720″ colwidth=”120|200|200|200″ colalign=”left|left|left|left”] Operator, Name, Example, Result
+, Addition ,10+3, 13
− ,Subtraction ,10−3, 7
/, Division, 10/3, 3.3333333333333
*, Multiplication, 10*3, 30
%, Modulus, 10%3, 1
[/table] 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 caption=”” width=”720″ colwidth=”120|150|150|150|150″ colalign=”left|left|left|left|left”] Operator, Name, Returns(True if),Example,Result
==, Equivalence, Left is equivalent to right,$x == 5, false
!=, Non-equivalence ,Left is not equivalent to right, $x != 5, true
===, Identical, Left is equivalent to right and they are the same type, $x === 5, false
>, Greater, Left is than greater than right,$x >4 ,false
>= ,Greater than or equal to, Left is greater than or equal to right,$x >= 4, true
< ,Less than, Left is less than right,x < 4 ,false
<= ,Less than or equal to,Left is less than or equal to right,$x <= 4, true
[/table] These operators are most commonly used with integers or doubles, although the equivalence operator is also used to compare strings.

Recommended Tutorials for you

Related posts

Looping Statement In PHP

Anuj Kumar

How to delete multiple records in PHP

Anuj Kumar

Session In PHP

Anuj Kumar

Get City Country By IP Address in PHP

Anuj Kumar

How to get Browser Information In PHP

Anuj Kumar

Useful functions for arrays in PHP

Anuj Kumar

How to Fetch data from database Using OOPS In PHP

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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