Curriculum
This tutorial will teach you about PHP operators and how to effectively use them in your script.
An operator performs a specific operation on one or more values known as operands.
The + operator, for example, adds two numbers and returns their sum.
PHP supports a wide range of operators, including:
Arithmetic Operators
Assignment Operators
Bitwise Operators
Comparison Operators
Increment/Decrement Operators
Logical Operators
Concatenating Operators
Numeric values are required by the arithmetic operators. When applied to non-numeric values, they will convert them to numeric values before performing the arithmetic operation.
The list of arithmetic operators is as follows:
Operator | Name | Description |
---|---|---|
+ | Addition | Return the sum of two operands |
– | Subtraction | Return the difference between two operands |
* | Multiplication | Return the product of two operands |
/ | Division | Return the quotient of two operands |
% | Modulus | Return the remainder of the division of the first operand by the second one |
The arithmetic operators are used in the following example:
<?php $x = 20; $y = 10; // add, subtract, and multiplication operators demo echo $x + $y . '<br/>'; // 30 echo $x - $y . '<br/>'; // 10 echo $x * $y . '<br/>'; // 200 // division operator demo $z = $x / $y; echo gettype($z) . '<br/>'; // integer $z = $y / $x; echo gettype($z) . '<br/>'; // double // modulus demo $y = 15; echo $x % $y . '<br/>'; // 5
You can use comparison operators to compare two operands.
A comparison operator returns either true or false as a Boolean value. The comparison operator returns true if the comparison is correct; otherwise, it returns false.
The following is a list of PHP comparison operators:
Operator | Name | Description |
---|---|---|
== | Equality | Return true if both operands are equal, otherwise returns false . |
=== | Identity | Return true if both operands have the same data type and equal, otherwise return false . |
!=== | Not identical | Return true if both operands are not equal or not have the same data type, otherwise returnfalse . |
> | Greater than | Return true if the operand on the left is greater than the operand on the right, otherwise return false . |
>= | Greater than or equal to | Return true if the operand on the left is greater than or equal to the operand on the right, otherwise return false . |
< | Less than | Return true if the operand on the left is less than the operand on the right, otherwise return false . |
<= | Less than or equal | Return true if the operand on the left is less than or equal to the operand on the right, otherwise return false . |
Logical operators are used to create logical expressions. A Boolean value is returned by a logical operator.
PHP includes the logical operators listed below:
Operator | Name | Description |
---|---|---|
&& | Logical AND | Return true if both operands are true , otherwise return false . If the first operand is false , it will not evaluate the second operand because it knows for sure that the result is going to be false . This is known as short-circuiting. |
|| | Logical OR | Return true if one of the operands is true , otherwise returns false . If the first operand is true , it will not evaluate the second one. |
xor | Logical XOR | Return true if either operand, not both, is true , otherwise, return false . |
! | Not | returns true if the operand is false , and returns false if the operand is true . |
Bitwise operators perform operations on the operands’ binary representations. The following PHP bitwise operators are illustrated:
Operators | Name | Result |
---|---|---|
$x & $y |
And | If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0 |
$x | $y |
Or (inclusive or) | If both bits are 0, the corresponding bit in the result is 0; otherwise, the corresponding bit is 1 |
$x ^ $y |
Xor (exclusive or) | If either bit, but not both, in $x and $y are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0 |
~ $x |
Not | Change bit 1 to 0 and 0 to 1 in the $x operand |
$x << $y |
Shift left | Shifts the bits in $x left by the number of places specified by $y . |
$x >> $y |
Shift right | Shifts the bits in $x right by the number of places specified by $y . |
The increment (++) and decrement (–) operators quickly increase and decrease the value of a variable by one.
The increment and decrement operators are illustrated in the table below:
Example | Name | Returned Value | Effect on $a |
---|---|---|---|
++$a |
Pre-increment | $a + 1 |
Increments $a by 1, then returns $a . |
$a++ |
Post-increment | $a |
Returns $a , then increments $a by 1. |
--$a |
Pre-decrement | $a - 1 |
Decrements $a by 1, then returns $a . |
$a-- |
Post-decrement | $a |
Returns $a , then decrements $a by 1. |
The concatenating operator (.) combines two strings into one. It joins the second and first strings and returns the combined string. As an example:
<?php $str = 'PHP' . ' is ' . ' Awesome!'; echo $str;
The assignment operator (=) both assigns and returns a value to a variable. The left operand is always a variable, whereas the right operand can be a literal value, variable, expression, or a function call that returns a value. As an example:
<?php $x = 10; $y = $x; $z = ($x = 20); // $z = 20
We assigned the variable $x the value 10 in the first expression. The value of $x was assigned to the $y variable in the second. The third is a little more difficult. First, we assigned $x a value of 20. The assignment operator (=) returns 20, which is then assigned to the variable $z.
Aside from the basic assignment operator (=), PHP includes the following assignment operators:
modulus-equal += minus-equal -= divide-equal /= multiplication-equal *= modulus-equal percent = XOR-equal = AND-equal &= OR-equal |= concatenate-equal.=
The precedence of an operator determines the order in which it is evaluated in an expression.
Each operator was given precedence by PHP. Some operators have the same precedence, for example, the precedences of addition (+) and subtraction (-) are the same.
However, some operators take precedence over others.
The precedence of the multiplication operator (*), for example, is higher than the precedence of the add(+) and subtract (-) operators:
<?php echo 4 + 5 * 3; // 19
Because the multiplication operator (*) has a higher precedence than the add(+) operator, PHP evaluates the multiplication operator (*) first and then the add operator (*) second.
To force the evaluation to take place in a specific order, enclose the expression in parentheses (), for example:
<?php echo (4 + 5) * 3; // 27