PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Operators are used to perform operations on variables and values. PHP divides the operators into the following groups:
1. Arithmetic operators
Arithmetic operators are used to perform common mathematical operations.
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulus)**
(Exponentiation)2. Assignment operators
Assignment operators are used to write a value to a variable.
=
(Assign)+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign).=
(Concatenate and assign)3. Comparison operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
==
(Equal)===
(Identical)!=
(Not equal)!==
(Not identical)<
(Less than)>
(Greater than)<=
(Less than or equal to)>=
(Greater than or equal to)<=>
(Spaceship)4. Increment/Decrement operators
Increment/decrement operators are used to increment/decrement a variable's value.
++$x
(Pre-increment)$x++
(Post-increment)--$x
(Pre-decrement)$x--
(Post-decrement)5. Logical operators
Logical operators are used to combine conditional statements.
and
(And)or
(Or)xor
(Xor)&&
(And)||
(Or)!
(Not)6. String operators
String operators are used to manipulate strings.
.
(Concatenation).=
(Concatenation assignment)7. Array operators
Array operators are used to compare arrays.
+
(Union)==
(Equality)===
(Identity)!=
(Inequality)<>
(Inequality)!==
(Non-identity)8. Conditional assignment operators
Conditional assignment operators are used to set a value depending on conditions.
?:
(Ternary)??
(Null coalescing)Example:
$x = 10; $y = 20; // Arithmetic operator echo $x + $y; // 30 // Assignment operator $x += $y; // $x = $x + $y; $x = 30 // Comparison operator if ($x == $y) { echo "Equal"; } else { echo "Not equal"; } // Increment operator $x++; // 31 // Logical operator if ($x > 0 and $y > 0) { echo "Both are positive numbers"; } // String operator $txt1 = "Hello"; $txt2 = "World"; echo $txt1 . $txt2; // HelloWorld // Array operator $array1 = array("a" => "red", "b" => "green"); $array2 = array("c" => "blue", "d" => "yellow"); print_r($array1 + $array2); // Union of $array1 and $array2 // Conditional assignment operator $user = isset($user) ? $user : 'guest'; // If $user is set then assign its own value, otherwise assign 'guest' $user = $user ?? 'guest'; // PHP 7+ version of the above code ``
Arithmetic operators in PHP:
+
, -
, *
, /
, and %
.$a = 5; $b = 2; $sum = $a + $b; // Addition $diff = $a - $b; // Subtraction $prod = $a * $b; // Multiplication $quot = $a / $b; // Division $mod = $a % $b; // Modulus (remainder)
Comparison operators in PHP:
==
, ===
, !=
, !==
, <
, >
, <=
, and >=
.$x = 5; $y = 10; $isEqual = ($x == $y); // Equal $isIdentical = ($x === $y); // Identical $isNotEqual = ($x != $y); // Not Equal $isNotIdentical = ($x !== $y); // Not Identical
Logical operators in PHP:
&&
(and), ||
(or), !
(not).$a = true; $b = false; $resultAnd = $a && $b; // Logical AND $resultOr = $a || $b; // Logical OR $resultNot = !$a; // Logical NOT
Assignment operators in PHP:
=
, +=
, -=
, *=
, /=
, and %=
.$x = 5; $y = 3; $x += $y; // Equivalent to $x = $x + $y; $x *= 2; // Equivalent to $x = $x * 2;
Bitwise operators in PHP:
&
, |
, ^
, ~
, <<
, and >>
.$a = 5; // binary: 0101 $b = 3; // binary: 0011 $bitwiseAnd = $a & $b; // binary: 0001 $bitwiseOr = $a | $b; // binary: 0111
Increment and decrement operators in PHP:
++
) and decrement (--
) operators.$num = 5; $num++; // Increment by 1 $num--; // Decrement by 1
Ternary (conditional) operator in PHP:
? :
) provides a shorthand for if-else statements.$isTrue = true; $result = $isTrue ? "It's true!" : "It's false!";
String concatenation operator in PHP:
.
operator is used for string concatenation.$str1 = "Hello"; $str2 = "World"; $concatenated = $str1 . " " . $str2; // Output: Hello World