Operator Types in PHP

What exactly is the Operator? A straightforward response can be provided by stating that the sum of 5 and 10 is equal to 15. The numbers 5 and 10 are referred to as operands, and the plus sign is the operator. The following types of operators are supported by the PHP language.

The following are the different types of operators:

arithmetic operators, comparison operators, logical operators (also known as relationship operators), assignment operators, and conditional operators (also known as ternary operators).

Let\’s go through each of the operators one at a time, shall we?

Operators for Arithmetic Operations

The PHP programming language provides support for the following arithmetic operators:

Assuming that variable J contains the value 5 and variable K contains the value 10, then

Some Examples

Operator Description Example
+ Adds two operands J + K will give 15
Subtracts second operand from the first J- K will give -5
* Multiply both operands J * K will give 50
/ Divide numerator by de-numerator K / J will give 2
% Modulus Operator and remainder of after an integer division K % J will give 0
++ Increment operator, increases integer value by one J++ will give 6
Decrement operator, decreases integer value by one J– will give 4

 

Comparison Operators

The PHP programming language provides support for the following comparison operators:

Assuming that variable J contains the value 10 and variable K contains the value 20, then

Some Examples

Operator Description Example
== Checks to see if the values of two operands are the same, and if they are, the condition is satisfied and the result is true. (J == K) is not true.
!= This operation determines whether or not the values of two operands are the same, and if they are not, the condition is considered to be satisfied. (J != K) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (J > K) is not true.
< Determines whether the value of the left operand is lower than the value of the right operand; if this is the case, the condition is satisfied and the result is true. (J < K) is true.
>= Determines whether the value of the left operand is larger than or equal to the value of the right operand; if this is the case, the condition is satisfied and the result is true. (J >= K) is not true.
<= Performs a test to see if the value of the left operand is lower than or equal to the value of the right operand; if the test is successful, the condition is satisfied and the result is true.. (J <= K) is true.

 

Logical Operators

The PHP programming language provides support for the following logical operators:

Assuming that variable J contains the value 10 and variable K contains the value 20, then

Some Examples

Operator Description Example
and (&) and is called Logical AND operator. If both the operands are true then condition becomes true. (J and K) is true.
or (|) or is called Logical OR Operator. If any of the two operands are non zero then condition becomes true. (J or K) is true.
&& && Called Logical AND operator. If both the operands are non zero then condition becomes true. (J && K) is true.
|| || Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. (J || K) is true.
!(NOT) ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(J && K) is false.

 

Operators for Assigning Objects

 

The PHP programming language provides support for the following assignment operators:

Providing Instances of Assignment Operators

The PHP programming language provides support for the following assignment operators:

Some Examples

Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand L = J + K will assign value of J + K into L
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand L += J is equivalent to L = L + J
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand L -= J is equivalent to L = L – J
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand L *= J is equivalent to L = L * J
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand L /= J is equivalent to L = L / J
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand L %= J is equivalent to L = L % J

 

Conditional Operator

An additional operator known as the conditional operator also exists. This first puts an expression through an evaluation to determine whether it returns a true or false value, and then it either executes the first or the second of the given statements, based on the outcome of the evaluation. The syntax for the operator that tests conditions looks like this:

Some Examples

Operator Description Example
? : Conditional Expression If Condition is true  then value J otherwise value K

Operators Categories

All of the operators that we have spoken about up until this point can be organised into the following categories:

  • The unary prefix operators are those that come before an individual operand.
  • Binary operators are operators that take two operands and perform a variety of arithmetic and logical operations on them. Binary operators are used in computer programming.
  • The conditional operator is a ternary operator that takes three operands and evaluates either the second or the third expression, depending on how the first expression is evaluated. It does this by taking three operands.
  • The operators known as assignment operators are responsible for giving a value to a variable.

The Priority Order of PHP Operators

  • The precedence of operators in an expression is what decides how the terms in the expression are grouped. The evaluation of an expression is altered as a result of this. Some operators have more precedence than others; for instance, the operator for performing multiplication has higher precedence than the operator for performing addition.
  • For example, L equals 7 plus 3 times 2 multiplied; in this case, L is given 13, not 20 since the precedence of the * operator is higher than that of the + operator; hence, it first gets multiplied with 3*2 and then adds into 7.
  • The operators in this table whose precedence is the highest can be found at the very top, while those whose precedence is the lowest can be found at the very bottom. When evaluating an expression, the operators with the highest precedence will be evaluated first.
Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top