Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.

Get Started Now!

PHP Operators and its types?

In PHP, Operators are special symbols that are used to perform operations on variables and values.

PHP has various types of operators-

  1. Arithmetic operator
  2. Comparison operator
  3. Logical operator
  4. Assignment operator
  5. Increment and Decrement operator
  6. Bitwise operator

Arithmetic operator in PHP:

Arithmetic operators Example like: +, -, *, /, % (modulus), ** (exponentiation)

OperatorNameExampleExplanation
+Addition$a + $bSum of operands
Subtraction$a – $bDifference of operands
*Multiplication$a * $bProduct of operands
/Division$a / $bQuotient of operands
%Modulus$a % $bRemainder of operands
**Exponentiation$a ** $b$a raised to the power $b

Assignment operator in PHP:

Assignment operators: =, +=, -=, *=, /=, %=, .= (concatenation assignment)

OperatorNameExampleExplanation
=Assign$a = $bThe value of right operand is assigned to the left operand.
+=Add then Assign$a += $bAddition same as $a = $a + $b
-=Subtract then Assign$a -= $bSubtraction same as $a = $a – $b
*=Multiply then Assign$a *= $bMultiplication same as $a = $a * $b
/=Divide then Assign
(quotient)
$a /= $bFind quotient same as $a = $a / $b
%=Divide then Assign
(remainder)
$a %= $bFind remainder same as $a = $a % $b

Comparison operators:

Comparison operators: ==, === (identical), !=, !== (not identical), <, >, <=, >=

OperatorNameExampleExplanation
==Equal$a == $bReturn TRUE if $a is equal to $b
===Identical$a === $bReturn TRUE if $a is equal to $b, and they are of same data type
!==Not identical$a !== $bReturn TRUE if $a is not equal to $b, and they are not of same data type
!=Not equal$a != $bReturn TRUE if $a is not equal to $b
<>Not equal$a <> $bReturn TRUE if $a is not equal to $b
<Less than$a < $bReturn TRUE if $a is less than $b
>Greater than$a > $bReturn TRUE if $a is greater than $b
<=Less than or equal to$a <= $bReturn TRUE if $a is less than or equal $b
>=Greater than or equal to$a >= $bReturn TRUE if $a is greater than or equal $b
<=>Spaceship$a <=>$bReturn -1 if $a is less than $b
Return 0 if $a is equal $b
Return 1 if $a is greater than $b

Logical operator:

Logical operators: && (and), || (or), ! (not)

OperatorNameExampleExplanation
andAnd$a and $bReturn TRUE if both $a and $b are true
OrOr$a or $bReturn TRUE if either $a or $b is true
xorXor$a xor $bReturn TRUE if either $ or $b is true but not both
!Not! $aReturn TRUE if $a is not true
&&And$a && $bReturn TRUE if either $a and $b are true
||Or$a || $bReturn TRUE if either $a or $b is true

Increment and Decrement operator:

Increment/Decrement operators: ++ (increment), — (decrement)

NameOperator
Pre-increment++$x
Post-Increment$x- –    
Pre-decrement–$x
Post-decrement$x- –

Bitwise operator:

Bitwise operators: & (and), | (or), ^ (xor), ~ (not), << (left shift), >> (right shift)

OperatorNameExampleExplanation
&And$a & $bBits that are 1 in both $a and $b are set to 1, otherwise 0.
|Or (Inclusive or)$a | $bBits that are 1 in either $a or $b are set to 1
^Xor (Exclusive or)$a ^ $bBits that are 1 in either $a or $b are set to 0.
~Not~$aBits that are 1 set to 0 and bits that are 0 are set to 1
<<Shift left$a << $bLeft shift the bits of operand $a $b steps
>>Shift right$a >> $bRight shift the bits of $a operand by $b number of places

Related Posts

How to Install Node.js and npm (Step-by-Step Guide)

How to Install Node.js and npm (Step-by-Step Guide) Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, and npm (Node Package Manager) is the package Read More

Read More

Creating a WordPress Post with Google Image Search Results in PHP

In this blog post, we will explore a PHP script that utilizes the Google Custom Search API to search for images based on user-defined keywords. We’ll also Read More

Read More

How to Automatically Generate Image Search Queries Based on Keywords Using Google API

To automatically generate image search queries using Google API, you can use the Custom Search JSON API. Here’s a step-by-step guide: Step 1: Create a Google Cloud Read More

Read More

How to Build an Image Search Engine with Google Custom Search API and PHP

Building an image search engine using the Google Custom Search API and PHP involves several steps. Here is a detailed guide: Prerequisites Step 1: Create a Custom Read More

Read More

What is Composer, and why is it used in PHP development?

Composer is a dependency management tool specifically designed for PHP. It streamlines the process of managing libraries and packages required for PHP projects, making development more efficient Read More

Read More

Understanding PHP Data Types: A Comprehensive Guide with Examples

In PHP, data types refer to the various kinds of data that can be stored and manipulated within the language. PHP is a loosely typed language, meaning Read More

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x