PHP Operator: Different Types of Operators In PHP

What is an Operator in PHP?

PHP Operators are symbols or keywords that let you work with variables and values in PHP (Hypertext Preprocessor).

PHP is a common programming language used on websites.

With these operators, coders can change or manipulate data, compare items, do math, and guide the flow of their programs.

Simply put, these operators can be used to apply an action to values or variables.

For Example:

$add= 15+ 70; (15and 70) are operands and (+) is the operator.

An operator in PHP takes (one or more) an expression or any value in programming and passes another value to become an expression itself.

What are the different types of operators in PHP?

There are Different Types of Operators in PHP:

  • Comparison operators
  • Conditional assignment operators
  • Arithmetic operators
  • Array operators
  • String operators
  • Assignment operators
  • Increment/Decrement operators
  • Logical operators
  • Spaceship Operators (Introduced in PHP 7)

Comparison Operators in PHP

Comparison operators in PHP are used to compare two values or expressions and return a boolean result (either true or false) based on the comparison.

These operators are fundamental for making decisions and controlling the flow of execution in PHP programs.

==Equal toReturns true if the values of two operands are equal. “$a == $b”
!=, <>Not Equal toReturns true if the values of two operands are not equal.
“$a != $b” or “$a <> $b”
===Identical toReturns true if both the value and the data type of the operands are identical. “$a === $b”
!==Not identical toReturns true if both the value and the data type of the operands are different. “$a !== $b”
>Greater thanReturns true if the value of the left operand is greater than the value of the right operand. “$a > $b”
>=Greater than or Equal toReturns true if the value of the left operand is greater than or equal to the value of the right operand. “$a >= $b”
<Less thanReturns true if the value of the left operand is less than the value of the right operand. “$a < $b”
<=less than or Equal toReturns true if the value of the left operand is less than or equal to the value of the right operand. “$a <= $b”

Conditional Assignment Operators in PHP

Conditional assignment operators in PHP are shorthand expressions that are used to assign values to variables based on certain conditions.

It combines conditional statements with assignment operations precisely and efficiently.

?:Ternary OperatorIf the condition evaluates to true, the value of value_if_true is assigned to $variable; otherwise, the value of value_if_false is assigned.
??Null Coalescing OperatorIf $value is set and not null, $variable is assigned the value of $value; otherwise, it’s assigned the value of $default_value.
$variable = (condition) ? value_if_true : value_if_false; //Using a Ternary Operator

$variable = $value ?? $default_value; //Using a Null Coalescing Operator

Arithmetic Operators in PHP

Arithmetic operators in PHP are used to perform mathematical operations on numeric values.

These operators allow you to manipulate numbers by adding, subtracting, multiplying, dividing, and more.

OperatorsNameOperationSyntax
+Additionadding the two operands together.$a + $b
SubtractionSubtracts the second operand from the first.$a – $b
*MultiplicationMultiplies two operands.$a * $b
/DivisionDivides the first operand by the second.$a / $b
%ModulusReturns the remainder of the division of the first operand by the second.$a % $b
**ExponentiationRaises the first operand to the power of the second.$a ** $b
++IncrementIncreases the value of a variable by one.$a++
DecrementDecreases the value of a variable by one.$b–

Array Operators in PHP

Array Operators in PHP are used to manipulate arrays, which are a fundamental data structure in PHP for storing and organizing data.

These operators provide functionality for combining, comparing, and modifying arrays.

OperatorNameOperationSyntax
+UnionReturns the union of two arrays, merging them together without duplicates from the second array. $p + $k
==EqualityCheck if the two arrays have the same key/value pairs.$p == $k
!=InequalityCheck if the two arrays do not have the same key/value pairs.$p != $k
===IdentityCheck if the two arrays are identical, meaning they have the same key/value pairs in the same order.$p === $k
!==Non-IdentityCheck if the two arrays are not identical.$p !== $k
<>InequalityCheck if the two arrays do not have the same key/value pairs. $p<>$k

String Operators in PHP

String operators in PHP are used to manipulate strings, which are sequences of characters.

These operators provide functionality for the concatenation operator and concatenating assignment operator.

OperatorsNameOperationSyntax
.ConcatenationCombines two strings together to create a single string.$p.$k
.=Concatenation and assignment
Appends one string to another and assigns the result to the first string.$p.=$k

Assignment Operators in PHP

Assignment operators in PHP are used to assign values ​​to variables.

These operators allow you to store data in variables and manipulate them as needed.

OperatorNameOperatorsSyntax
=AssignAssigns the value on the right-hand side to the variable on the left-hand side.$a = $b
+=Add then AssignAdds the value of the right operand to the variable and assigns the result to the variable.$a += $b
-=Subtract then AssignSubtracts the value of the right operand from the variable and assigns the result to the variable.$a -= $b
*=Multiply then AssignMultiplies the variable by the value of the right operand and assigns the result to the variable.$a *= $b
/=Divide then Assign (quotient)Divides the variable by the value of the right operand and assigns the result to the variable.$a /= $b
%=Divide then Assign (remainder)Divides the value of the variable by the value of the right operand and assigns the remainder of the division to the variable.$a %= $b

Increment/Decrement Operators in PHP

Increment and decrement operators in PHP are used to increase or decrease the value of a variable by one, respectively.

These operators provide a shorthand way to perform these common operations.

OperatorNameOperationSyntax
++Pre-Incrementincrements the value of the variable by one, and then returns the incremented value.++$pre
Pre-Decrementdecrements the value of the variable by one, and then returns the decremented value.–$pre
++Post-Incrementreturns the current value of the variable, and then increments the value of the variable by one.$post++
Post-Decrementreturns the current value of the variable, and then decrements the value of the variable by one.$post–

Spaceship Operators in PHP

The spaceship operator (PHP 7) (<=>) in PHP, also known as the three-way comparison operator, is used for comparing two expressions or values as whether the first value is greater than, equal, or less than the 2nd value.

It returns one of three values:

  • -1: if the left operand is less than the right operand,
  • 0: if the operands are equal, and
  • 1: if the left operand is greater than the right operand.

Condition SyntaxReturn Value
$p < $k$p <=> $k-1 ($k is greater)
$p > $k$p <=> $k1 ($p is greater)
$p == $k$p <=> $k0 (both are equal)

How is the Ternary Conditional Operator used in PHP?

The ternary conditional operator in PHP, represented by the syntax “?:”, is a shortened version of the if-else statement. 

It is used to evaluate a condition and return one of two values depending on whether the condition is true or false.

The syntax of the ternary operator is:

(condition) ? true_value : false_value;

Here’s how it works:

  • The condition is evaluated first.
  • If the condition is true, the value after the “?” is returned.
  • If the condition is false, the value after the “:” is returned.

$age = 20;
$message = ($age >= 18) ? "You are an adult" : "You are a minor";
echo $message;

  • If the value of $age is greater than or equal to 18, the message “You are an adult” is assigned to $message.
  • If the value of $age is less than 18, the message “You are a minor” is assigned to $message.

The ternary operator is often used in situations where a simple decision needs to be made based on a condition, and it helps to keep code concise and readable.

Conclusion

This article covers various types of operators in PHP. It provides a comprehensive overview of each type of operator and their functionalities, serving as a valuable resource for PHP developers.

By exploring these operators, readers gain a deeper understanding of how to manipulate data and control program flow effectively in PHP.

For more insightful tutorials and articles on PHP development, readers are encouraged to explore the author’s previous and latest works.

Leave a Comment