- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
Operators in PHP with types and examples
Operators are special symbols or keywords in PHP that carry out specific operations on variables or values. Arithmetic, comparison, logical, assignment, and bitwise operators are all supported by PHP.
Types of operators in PHP
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Bitwise operators
- Increment/Decrement operators
- Conditional operators
These are the main operators in PHP. Let's explain these operators one by one.
Arithmetic operators in PHP
Arithmetic operators carry out mathematical operations on numbers. The following arithmetic operators are supported by PHP:
Operator | Description |
---|---|
+ | Adds two values. |
- | This operator subtracts one value from another. |
* | Multiplies two values. |
/ | This operator divides one value by another. |
% | Returns the remainder of a division operation. |
PHP arithmetic operator example
<?php $a = 11; $b = 5; echo $a + $b; echo "<BR>"; echo $a - $b; echo "<BR>"; echo $a * $b; echo "<BR>"; echo $a / $b; echo "<BR>"; echo $a % $b; ?>
16 6 55 2.2 1
Comparison operators in PHP
The comparison operator compares two values and returns a Boolean result. The following comparison operators are supported by PHP:
Operator | Description |
---|---|
== | If both values are equal, this operator returns true or 1. |
!= | If both values are not equal, this operator returns true. |
=== | If both values are equal and of the same type, this operator returns true. |
!== | Returns true if neither value is equal nor of the same type. |
> | If the left value is greater than the right value, this operator returns true. |
< | If the left value is less than the right value, this operator returns true. |
>= | If the left value is greater than or equal to the right value, this operator returns true. |
<= | If the left value is less than or equal to the right value, this operator returns true. |
PHP comparison operators example
<?php $a = 11; $b = 5; $c = 11; echo $a == $c; echo "<BR>"; echo $a != $b; echo "<BR>"; echo $a === $c; echo "<BR>"; echo $a !== $b; echo "<BR>"; echo $a > $b; echo "<BR>"; echo $b < $c; echo "<BR>"; echo $a >= $b; echo "<BR>"; echo $b <= $c; ?>
1 1 1 1 1 1 1 1
Logical operators in PHP
A Boolean result is produced by combining two or more Boolean expressions using logical operators. The following logical operators are supported by PHP:
Operator | Description |
---|---|
&& | If both expressions are true, this operator returns true. |
|| | If at least one expression is true, this operator returns true. |
! | If the expression is true, this operator returns false, and vice versa. |
PHP logical operators example
<?php $a = true; $b = false; $c = true; echo $a && $c; echo "<BR>"; echo $a || $c; echo "<BR>"; echo !$b; ?>
1 1 1
Assignment operators in PHP
Variables can have values assigned to them using assignment operators. The following assignment operators are supported by PHP:
Operator | Description |
---|---|
= | assigns a variable a value. |
+= | Adds a value to a variable and then returns the result. |
-= | The operator subtracts a value from a variable and assigns the result to the variable. |
*= | A variable is multiplied by a value and the result is assigned to the variable. |
/= | The result of dividing a variable by a value is assigned to the variable. |
%= | The remainder of a variable divided by a value is calculated and assigned to the variable. |
PHP assignment operators example
<?php $a = 11; $b = 5; $a += $b; echo $a; echo "<BR>"; $a -= $b; echo $a; echo "<BR>"; $a *= $b; echo $a; echo "<BR>"; $a /= $b; echo $a; echo "<BR>"; $a %= $b; echo $a; ?>
16 11 55 11 1
The following statement:
$a += $b;
is equivalent to
$a = $a + $b;
Similarly, the following statement:
$a -= $b;
is equivalent to
$a = $a - $b;
and so on.
Bitwise operators in PHP
Bitwise operators operate on numeric values' binary representations. PHP supports the bitwise operators listed below:
Operator | Description |
---|---|
& | when both operands are 1, this function returns a 1 in every bit position. |
! | Returns a 1 in each bit position where either operand has a 1. |
^ | Returns a 1 in every case where there is only one 1 in the operand. |
˜ | bits in its operand are inverted. |
<< | by the number of positions specified by the right operand, shifts the bits of the left operand to the left. |
>> | by the number of positions specified by the right operand, shifts the bits of the left operand to the right. |
PHP bitwise operators example
<?php $a = 10; $b = 5; echo $a & $b; echo "<BR>"; echo $a | $b; echo "<BR>"; echo $a ^ $b; echo "<BR>"; echo ˜$a; echo "<BR>"; echo $a << 1; echo "<BR>"; echo $a >> 1; ?>
0 15 15 -11 20 5
The binary equivalent of 10 is 1010, 5 is 0101, 0 is 0000, 15 is 1111, -11 is 11110101, and 20 is 10100.
Increment/Decrement operators in PHP
The increment/decrement operators in PHP are used to change a variable's value by one. Since they only operate on one variable, they are categorized as unary operators.
A variable's value is increased by the increment operator (++) by 1 and decreased by the decrement operator (--) by 1. The order in which these operators are used can have an impact on the operation's outcome. These operators can be used either before or after the variable they are operating on.
This type of increment/decrement operator is known as a pre-increment/pre-decrement operator because it is applied before the variable. The variable's value is altered before it is plugged into the expression. Post-increment or post-decrement operators are those that come after the variable they are modifying. Here, the variable's value is changed after it has been used in an expression.
PHP Increment/Decrement operators example
<?php $a = 5; echo ++$a; // increments, then use echo "<BR>"; echo $a; echo "<BR>"; echo $a++; // use, then increments echo "<BR>"; echo $a; echo "<BR>"; $b = 5; echo --$b; // decrements, then use echo "<BR>"; echo $b; echo "<BR>"; echo $b--; // use, then decrements echo "<BR>"; echo $b; ?>
6 6 6 7 4 4 4 3
Conditional operators in PHP
The conditional operator in PHP is a one-line shortcut for writing an if-else statement. The syntax is as follows:
condition ? x : y;
The "condition" will be evaluated first; if it is evaluated as true, then "x" will be returned, otherwise "y" will be returned.
PHP conditional operator example
<?php $a = 5; $b = 10; $c = $a>$b ? "codes" : "cracker"; echo $c; echo "<BR>"; $d = $a<$b ? "codes" : "cracker"; echo $d; ?>
cracker codes
The step-by-step explanation of the previous code is:
- $a is assigned the value 5
- $b is assigned the value 10
- $c is assigned the value "codes" if $a is greater than $b, and "cracker" otherwise. This is done using the ternary operator ? :
- $d is assigned the value "codes" if $a is less than $b, and "cracker" otherwise. Again, this is done using the ternary operator
The code then prints out the values of $c and $d separated by a line break (<BR>).
Since $a is not greater than $b, $c is assigned the value "cracker". And, since $a is less than $b, so $d is assigned the value "codes".
« Previous Tutorial Next Tutorial »