Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Arithmetic Operators
3.
Assignment Operators
4.
Bitwise Operators
5.
Comparison Operators
6.
Incrementing/Decrementing Operators
7.
Logical Operators
8.
String Operators
9.
Array Operators
10.
Execution Operators
11.
Error Control Operators
12.
Conditional Assignment Operators
13.
Frequently Asked Questions
14.
Key Takeaways
Last Updated: Mar 27, 2024

PHP Operators

Author Abhay Trivedi
0 upvote

Introduction

PHP Operators are symbols that we use to perform operations on operands. We can classify PHP operators on behalf of operands into three types:

  • Unary Operators - It works on single operands. For eg. ++, -- etc.
  • Binary Operators - It works on two operands. For eg. binary +, -, *, / etc.
  • Ternary Operators - It works on three operands. For eg. "?:".

We can classify PHP Operators in the following forms:-

  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Comparison Operators
  • Incrementing/Decrementing Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Type Operators
  • Execution Operators
  • Error Control Operators

Arithmetic Operators

We use Arithmetic Operators to perform arithmetic procedures such as addition, subtraction, multiplication, etc.

  • Addition (+) - Sum of operands.
    • For Example: $a + $b
  • Subtraction (-) - Difference of operands.
    • For Example: $a - $b
  • Multiplication (*) - Product the operands.
    • For Example: $a * $b
  • Division (/) - Quotient of operands.
    • For Example: $a / $b
  • Modulus (%) - Remainder of operands.
    • For Example: $a % $b
  • Exponentiation (**) - $a raised to the power $b.
    • For Example: $a ** $b

Assignment Operators

We use the assignment operator for assigning values to different variables.

  • Assign (=) - Assigns the value of the correct variable to the left variable.
    • For Example: $a = $b
  • Add then Assign (+=) - Addition done as $a = $a + $b.
    • For Example: $a += $b
  • Subtract then Assign (-=) - Subtraction done as $a = $a - $b.
    • For Example: $a -= $b
  • Multiply then Assign (*=) - Multiplication done as $a = $a * $b.
    • For Example: $a *= $b
  • Divide then Assign quotient (/=) - Gives quotient as $a = $a / $b.
    • For Example: $a /= $b
  • Divide then Assign reminder (%=) - Gives reminder as $a = $a % $b.
    • For Example: $a %= $b

Bitwise Operators

We use Bitwise operators to perform bit-level operations on operands.

  • AND (&) - Bits that are 1 in both $a and $b are set to 1, else set to 0.
    • For Example: $a & $b
  • OR (&) - Bits that are either 1 in $a and $b are set to 1, else set to 0.
    • For Example: $a | $b
  • XOR (^) - Bits that are 1 in both $a and $b are set to 0, else set to 1.
    • For Example: $a ^ $b
  • NOT (~) - Bits that are 1 are set to 0, and 0 is set to 1.
    • For Example: ~$a
  • Left shift (<<) - Left shift the bits of $a by $b steps.
    • For Example: $a << $b
  • Right shift (>>) - Right shift the bits of $a by $b steps.
    • For Example: $a >> $b

 

Read about Bitwise Operators in C here.

Comparison Operators

We use comparison operators for comparing two values such as number or string.

  • Equal (==) - Returns TRUE if $a is equal to $b.
    • For Example: $a == $b
  • Identical (===) - Returns TRUE if $a is equal to $b, along with their data types.
    • For Example: $a === $b
  • Not Equal (!=) - Returns TRUE if $a is not equal to $b.
    • For Example: $a != $b
  • Not Equal (<>) - Returns TRUE if $a is not equal to $b.
    • For Example: $a <> $b
  • Not Identical (!==) - Returns TRUE if $a is not equal to $b, along with their data types are not equal.
    • For Example: $a !== $b
  • Less than (<) - Returns TRUE if $a is less than to $b.
    • For Example: $a < $b
  • Greater than (>) - Returns TRUE if $a is greater than to $b.
    • For Example: $a > $b
  • Less than or equal to (<=) - Returns TRUE if $a is less than or equal to $b.
    • For Example: $a <= $b
  • Greater than or equal to (>=) - Returns TRUE if $a is greater than or equal to $b.
    • For Example: $a >= $b
  • Spaceship (<=>) - Return -1 if $a is less than $b, 0 if $a is equal $b, 1 if $a is greater than $b.
    • For Example: $a <=> $b

Incrementing/Decrementing Operators

We use these operators for incrementing and decrementing the value of a variable.

  • Post Increment (++) - Increments the value of $a by one, then return $a.
    • For Example: $a++
  • Pre Increment (++) - Return $a, thenIncrements the value of $a by one.
    • For Example: ++$a
  • Post Decrement (--) - Decrements the value of $a by one, then returns $a.
    • For Example: $a-- 
  • Pre Decrement (--) - Return $a, then decrements the value of $a by one.
    • For Example: --$a

Logical Operators

We use logical operators to perform bit-level operations on operands.

  • And (and, &&) - Return TRUE if $a is equal to $b.
    • For Example: $a and $b
    • For Example: $a && $b
  • Or (or, ||) - Return TRUE if either $a or $b or both are true.
    • For Example: $a or $b
    • For Example: $a || $b
  • Xor (xor) - Return TRUE if either $a or $b are true, but not both.
    • For Example: $a xor $b
  • Not (!) - Return TRUE if $a is not true.
    • For Example: !$a

String Operators

We use string operators to perform operations on strings.

  • Concatenation (.) - Concatenate both strings $a and $b.
    • For Example: $a.$b
  • Concatenation and Assignment (.=) - Concatenate both strings $a and $b, then assign it to $a like $a = $a.$b
    • For Example: $a.=$b

Array Operators

We use array operators to compare the values of arrays.

  • Union (+) - Union of $a and $b.
    • For Example: $a + $b
  • Equality (==) - Return TRUE if $a and $b have same key/value pair.
    • For Example: $a == $b
  • Inequality (!=) - Return TRUE if $a is not equal to $b.
    • For Example: $a != $b
  • Identity (===) - Return TRUE if $a and $b have same key/value pair of same type in same order.
    • For Example: $a === $b
  • Non-Identity (!==) - Return TRUE if $a is not identical to $b.
    • For Example: $a !== $b
  • Inequality (<>) - Return TRUE if $a is not equal to $b.
    • For Example: $a <> $b

Execution Operators

PHP uses backticks to run shell commands.

  • Backticks(``) - Execute the shell command and return the result.
    • For Example: echo `dir`;

Error Control Operators

When we use the error control operator with an expression, the error message gets ignored by that expression.

  • at(@) - Intentional file error.
    • For Example: @file('file_not_found')

Conditional Assignment Operators

Conditional Assignment Operators are used to setting values based on the following situations.

  • Ternary(?:) - Returns the value of $a, expr2 if expr1 = TRUE or expr3 if expr1 = FALSE.
    • For Example: $a = expr1 ? expr2 : expr3
  • Null coalescing (??) - Returns the value of $a, expr1 if expr1 exists and not NULL, else expr2.
    • For Example: $a = expr1 ?? expr2

Frequently Asked Questions

1. What is PHP?

PHP is a backend scripting language used for web development. It is an open-source, interpreted server-side scripting language and supports object-oriented programming.

2. What are the advantages of using PHP?

There are many advantages of using PHP over other languages. PHP is a server-side scripting language suited for creating dynamic web pages. It is highly flexible and provides compatibility and easy integration. It provides efficiency in performance and is cost-efficient. 

3. What are PHP Operators?

PHP Operators are symbols that we use to operate the value of a variable or a value. There are many PHP operators like arithmetic, logical, conditional, relational, bitwise, assignment operators, etc.

Key Takeaways

This article teaches about PHP Operators and how we use them. We saw why it could be beneficial for a developer to learn. Click here to read about PHP Interview Questions.

Click here to see other related blogs on PHP.

Also, check out our web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

 

By Abhay Trivedi

Live masterclass