PHP Operators Explained (With Examples)
Operators are used to perform various operations on variables and values. In this article we will learn about different types of PHP Operators and see how we can use them to perform various operations on variables and values.
Types of PHP Operators
There are several types of operators in PHP that allows you to perform various operations on data. These operators are:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- String Operators
- Array Operators
- Increment/Decrement Operators
PHP Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operations (addition, subtraction, exponentiation etc.) on variables or values. Here is a list of arithmetic operators along with their usage.
Name | Operator | Example | Result |
---|---|---|---|
Addition | + | $x + $y | Sum the values of $x and $y. |
Subtraction | – | $x – $y | Subtract the value of $y from $x. |
Multiplication | * | $x * $y | Multiples the values of $x by $y |
Division | / | $x / $y | Outputs the quotient when $x is divided by $y. |
Modules | % | $x % $y | Outputs the reminder when $x is divided by $y. |
Exponentiation | ** | $x ** $y | calculates the result of raising $x to the power of $y. |
Here is how we can use these operators in our code.
<?php //addition on variables $x = 5; $y = 2; echo ($x + $y); // Outputs: 7 //addition on values echo (5 + 2); // Outputs: 7 //Multiplication on variables $x = 2; $y = 3; echo ($x * $y); // Outputs: 6 //Multiplication on values echo (2 * 5); // Outputs: 10 //Exponentiation on variables $x = 3; $y = 2; echo ($x ** $y); // Outputs: 9 //Exponentiation on values echo (4 ** 2); // Outputs: 16 ?>
PHP Assignment Operators
Assignment Operators are used to assign any type of value to a variable. The sign of assignment operator is ‘=’. Here is a simple example:
<?php $x = 2; /* Here we have assign a value 2 to a variable $x.*/ ?>
One more important part about assignment operator is Compound Assignment Operator. Compound Assignment Operator are used to perform an operation and assign a result to a variable in a more concise manner. Here is a detailed overview about PHP Compound Assignment Operators.
Statement | Also Written As | Result |
---|---|---|
$x = $x + 2 | $x + = 2 | Adds the value 2 to a variable $x |
$x = $x – 2 | $x – = 2 | Subtracts the value 2 from the variable $x |
$x = $x * 2 | $x * = 2 | Multiplies the value of variable $x with 2 |
$x = $x / 2 | $x / = 2 | Divides the value of variable $x by 2 |
$x = $x ** 2 | $x ** = 2 | calculates the square of the value stored in variable $x |
$x = $y | $x = $y | Assigns the value of variable $x to a variable $y |
Here is how we can use the Compound Assignment Operators in our code.
<?php $x = 5; $x += 2; echo $x; //Outputs: 7 $x = 5; $x -= 2; echo $x; //Outputs: 3 $x = 4; $y = 2; $x **= $y; echo $x; //Outputs: 16 ?>
PHP Comparison Operators
Comparison Operators in PHP are used to compare two values and determine whether they are equal, not equal, greater than or less than. Comparison Operators returns the result in Boolean values, that is either true(1) or false(0). Here is a list of comparison operators in PHP.
Name | Operator | Example | Description |
---|---|---|---|
Equal | == | $x == $y | Returns true if the variable x is equal to y. |
Not Equal | != or <> | $x != $y | Returns 1 if the variables x and y holds equal values, otherwise 0. |
Identical | === | $x === $y | Returns 1 if the variables x and y holds equal values and are of same data type, otherwise 0. |
Not Identical | !== | $x !== $y | Returns true if the variables x and y holds equal values but are of different data type, otherwise false. |
Greater than | > | $x > $y | Returns True if the value of $x is greater than $y. |
Greater than or equal to | >= | $x >= $y | Returns True if the value of $x is greater than or equal to $y, otherwise false. |
Less than | < | $x < $y | Returns True if the value of $x is less than $y. |
Less than or equal to | <= | $x <= $y | Returns True if the value of $x is less than or equal to $y. |
Spaceship | <=> | $x <=> | If $x is less than, equal to or greater than $y it will return an integer less than, equal to or greater than $y. |
Here is how we can use these comparison operators in PHP.
<?php $x = 4; $y = "4"; echo $x == $y; //Outputs True echo $x === $y; //Outputs False echo $x > $y; /Outputs false echo $x >= $y; /Outputs true ?>
PHP Logical Operators
Logical Operators in PHP are used to perform logical operations on values or expressions. There are mainly three logical operators in PHP, ‘AND’, ‘OR’, and ‘NOT’. Here is a detailed overview about these three logical operators.
Name | Operator | Example | Description |
---|---|---|---|
AND | and, && | $x && $y | Outputs true if both the $x and $y are True. |
OR | or, || | $x || $y | Outputs true if any one or both $x and $y are true. |
NOT | ! | !$x > $y | Outputs true if $x is less than $y. |
Here is how we can use Logical Operators in PHP.
<?php $x = 4; $y = 6; echo ($x && $y > 10); //Outputs: False echo ($x && $y > 1); //Outputs: true echo ($x && $y > 5); //Outputs: false echo ($x or $y > 5); //Outputs: true echo ($x || $y > 10); //Outputs: false echo !($x > $y); //Outputs: true ?>
String Operators
String Operators are used to manipulate and combine strings. Here is a list of PHP operators that are used for this purposes.
Name | Operator | Example | Description |
---|---|---|---|
Concatenation | . | $str1 . $str2 | Combine $str1 and $str2 into a single string. |
Concatenation Assignment | .= | $str2 .= $str1 | appends a str1 in a str2 variable. |
Repetition | str_repeat | str_repeat(str, 3) | Repeats a string namely str 3 times. |
Here is how we can use these operators in our PHP code.
<?php $str1 = "Hello"; $str2 = " World"; $final = $str1 . $str2; echo $final; //Outputs: Hello World // Concatenation Assignment $str1 .= " World!"; echo $str1; //Outputs: Hello World! // String Repetition $str = "crus4"; $result = str_repeat($str, 3); echo $result; //Outputs: crus4crus4crus4 ?>
PHP Increment and decrement
PHP Increment and Decrement Operators are used to increase and decrease the value of a variable. Below is a list of PHP increment and decrement operators.
Name | Operator | Example | Description |
---|---|---|---|
pre-increment | ++$x | ++$x | Increases the value of $x by one, before it is used in expression. |
post-increment | $x++ | $x++ | Increases the value of $x by one, after it is used in an expression. |
pre-decrement | –$x | –$x | decreases the value of a variable $x by one, before it is used in an expression. |
post-decrement | $x– | $x– | decreases the value of a variable $x by one, after it is used in an expression. |
Here is how we can use increment and decrement operators in our code.
<?php //pre increment $x = 5; echo ++$x; //Outputs: 6 // post increment $x = 5; echo $x++; //Outputs: 5 //pre decrement $x = 5; echo --$x; //Outputs: 4 //post decrement $x = 5; echo $x--; //Outputs: 5 ?>
Array Operators
In all programming languages, Arrays are used to store multiple values in a single variable. We can use different operators to manipulate and work with arrays. Like an array union operator (+) to combine two arrays into a new array. We will learn in-depth about these operators in our array tutorial.
Share This Post!