PHP Operators
Operators are PHP symbols that are used to perform operations on variables and values. They instruct PHP to do out logical or mathematical operations. Operators transform a construction into an expression by taking one or more values, known as operands (or arguments), and returning another value. Expressions are everything that has a value and are the fundamental units of language. Expressions can be as basic as variables or constants, or they can be more complicated by combining operators and identifiers.

Arithmetic Operators
Operators in Arithmetic Standard mathematical operations are carried out via arithmetic operators. The majority combine two operands, making them binary operators. Nevertheless, some operate on a single operand and are unary operators, such as arithmetic negation. Nonnumeric values are transformed into numbers in accordance with certain principles, and arithmetic operators often require numeric values.
- + Addition: Used to sum two operands
- – Subtraction: Provides the two operands’ difference. Arithmetic Negation, a unary operator that multiplies the operand by -1, is another application for it.
- * Multiplication: Used to find the product of two operands
- / Division: Provides the two operands’ quotient. Depending on the outcome, dividing integers can produce either an integer or a floating-point value.
- % Modulus: Returns the remainder of the division of the first operand by the second operand after converting both operands to integers.
- ** Exponentiation: Raising the first operand to the power of the second is known as exponentiation.
Example:
<?php
$num1 = 10;
$num2 = 3;
echo "Addition: " . ($num1 + $num2) . "<br/>"; //
echo "Subtraction: " . ($num1 - $num2) . "<br/>"; //
echo "Multiplication: " . ($num1 * $num2) . "<br/>"; //
echo "Division: " . ($num1 / $num2) . "<br/>"; // 3.333.
echo "Modulus: " . ($num1 % $num2) . "<br/>"; // 1
echo "Exponentiation: " . ($num1 ** $num2) . "<br/>"; // 1000
echo "Negation: " . (-$num1) . "<br/>"; // -10
?>
Assignment Operators
Operators for Assignments A variable is given a value by assignment operators. The most basic operator is (=), which assigns the right-hand expression to the left-hand variable.
Additionally, PHP allows for coupled assignment operators, which modify a variable and then return the outcome to the original variable. These are composites of an equal sign and an operator.
Among the examples are:
- += (assign and add)
- -= (Assign and subtract)
- *= (Assign and multiply)
- /= (Assign and divide)
- %= (assign and modulus)
- .= (Assign and concatenate)
Example:
<?php
$total = 50;
$total += 10; // Adds 10 to $total, result is 60
echo "Total after += 10: " . $total . "<br/>"; // 60
$message = "Hello";
$message .= " World"; // Appends " World" to $message
echo "Message after .= \" World\": " . $message . "<br/>"; // Hello World
?>
Comparison Operators
Operators for Comparison With the exception of the spaceship operator, comparison operators compare two values and return a Boolean result (TRUE or FALSE). They are frequently utilised in conditional expressions.
Important comparison operators consist of:
- == (Equal): If values are equal, then TRUE.
- != or <> (Not equal): If values are not equal, this is TRUE.
- === (Identical): TRUE if the values are of the same type and equal.
- !== (Not identical): TRUE in the event that values are either not of the same type or not equal.
- < (Less than): TRUE if left operand is less than right operand.
- > (Greater than): Returns TRUE if the left operand is greater than the right.
- <= (Less than or equal to): Returns TRUE if the left operand is less than or equal to the right.
- >= (Greater than or equal to): Returns TRUE if the left operand is greater than or equal to the right.
- <=> (Spaceship): Introduced in PHP 7. Compares two operands ($a and $b) and returns an integer: less than 0 if $a is less than $b, 0 if $a equals $b, and greater than 0 if $a is greater than $b., empty strings, and NULL are regarded as FALSE, whereas non-zero numerical values and non-empty strings are regarded as TRUE.
Example:
<?php
$val1 = 10;
$val2 = "10";
$val3 = 20;
var_dump($val1 == $val2); // bool(true) - Value is equal
var_dump($val1 === $val2); // bool(false) - Value is equal, but type is not
var_dump($val1 < $val3); // bool(true)
var_dump($val3 >= $val1); // bool(true)
var_dump($val1 <=> $val3); // int(-1) - $val1 is less than $val3
?>
Logical Operators
If both operands are TRUE, then •&& or and (Logical AND): TRUE. Although they have different antecedents, && and and are operationally identical.
- || or or (Logical OR): Returns TRUE if either operand is TRUE. || and or also have different precedence.
- xor (Logical XOR): Returns TRUE if exactly one operand is TRUE.
- ! (Logical NOT): This is a unary operator that reverses the Boolean value of its operand; TRUE becomes FALSE and vice versa.
Example:
<?php
$user_is_active = true;
$is_premium = false;
var_dump($user_is_active && !$is_premium); // bool(true) - Active AND not premium
var_dump($user_is_active || $is_premium); // bool(true) - Active OR premium
var_dump(!$user_is_active); // bool(false) - NOT active
?>
Other Operators Other operators included in PHP include:
Bitwise Operators: These operators work with the arguments’ bitwise representation. Arguments are changed to integers unless they are strings. & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift) are examples of operators.
Ternary Operator (?:): PHP’s ternary operator (?:) is a single operator that accepts three operands. It returns the value of the second operand if the condition (first operand) is true; if not, it returns the value of the third operand. The statement is comparable to an if-else statement.
String Concatenation Operator (.): Two strings can be joined together using the String Concatenation Operator (.). The result is assigned to the left and the right argument is appended to the left using the combined assignment operator.=.
Incrementing/Decrementing Operators (++, –): The shorthand for increasing or removing one from a variable is the increment/decrement operators (++, –). When the variable is modified in relation to the evaluation of the expression, they might be positioned either before (prefix) or after (postfix).
Silence Operator (@): Used to suppress errors generated by an expression.
Casting Operators: Casting operators, such as (int), (string), force a value to be converted to a certain data type.
Execution Operator (“): Executes a shell command.
Instanceof Operator: Checks if an object is an instance of a particular class or implements a specific interface32….
Null Coalescing Operator (??): Introduced in PHP 7 (Details on its operation are not extensively covered in the provided beyond its existence).
Example (Ternary and String):
<?php
$is_member = true;
$discount = $is_member ? 10 : 0; // Ternary operator
echo "Your discount is: " . $discount . "%<br/>"; // String concatenation
?>
Example (Increment/Decrement):
<?php
$counter = 5;
echo "Prefix increment (++counter): " . (++$counter) . "<br/>"; // counter becomes 6, then outputted
echo "Value after prefix increment: " . $counter . "<br/>"; // 6
$index = 5;
echo "Postfix increment (index++): " . ($index++) . "<br/>"; // index (value 5) is outputted, then becomes 6
echo "Value after postfix increment: " . $index . "<br/>"; // 6
?>
Operator Precedence and Associativity Operator precedence
Operator Associativity and Precedence When brackets are not used to enforce the order, operator precedence controls the order in which operators are evaluated within an expression. Higher precedence operators are assessed before lower precedence operators. Division and multiplication usually trump addition and subtraction.
Associativity determines how precedence-based operators are assessed. Irrelevant, left-to-right, or right-to-left are typical.
To compel a particular order of operations and override the default precedence, use brackets ().
Example:
<?php
// Multiplication (*) has higher precedence than addition (+)
echo "Result of 3 + 2 * 7: " . (3 + 2 * 7) . "<br/>"; // Evaluates as 3 + (2 * 7) = 17
// Using parentheses to change precedence [32, 33, 73, 76-78]
echo "Result of (3 + 2) * 7: " . ((3 + 2) * 7) . "<br/>"; // Evaluates as (3 + 2) * 7 = 35
?>
Evaluating Expressions
Assessing Expressions Operators, variables, literals, and identifiers are combined to create expressions. Determining an expression’s final value is known as evaluation. The order of operations is determined by this evaluation using the operator precedence and associativity criteria. The output of an expression can subsequently be allocated to a variable or used as the operand for another operator. Any piece of code can be used to create complex expressions from simpler ones.
Example using eval():
<?php
eval("echo 'Hello World';"); // Evaluates the string and outputs "Hello World"
$code_from_database = '<?php print(date("Y-m-d")); ?>';
eval("?>" . $code_from_database); // Example simulating code from a database, requires careful handling of PHP tags
?>
Variables set in eval() will remain in the main script. eval() can return a value if the string has a return statement.
To write functional and predictable code in PHP, you must grasp the operators and how they affect the evaluation process, including precedence and associativity.