Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

What is PHP Operators and its Classification

php operators


Operators

All programming languages use operators. An expression consists of operators and operands. Operators are pre-defined symbols that perform specific actions on objects called operands. The operators are assigned a precedence value that indicates the order in which the operators are evaluated in an expression.

Classification of Operators

In PHP, operators are classified into the following categories:

  • Unary Operators - acts on only one operand in an expression.

  • Binary Operator - has two operands and performs different arithmetic and logical operations.

  • Conditional or ternary operators - have tree operands and evaluate the second or third expression depending on the result of the first expression.


1. Arithmetic Operators

Arithmetic operators are binary operators that work only on numeric operands. If the operands are non-numeric values such as strings, Booleans, nulls, or resources, they are converted to their numeric equivalents before evaluating the expression. To display 0 mathematical expressions.

Operator | Name | Description

  • + | Addition | Returns sum of operands
  • - | Subtraction | Returns the difference between two operands
  • * | Multiplication | Returns product of two
  • / | Division | Returns quotient after dividing the first operands by the second operand.
  • % | Modulus | Returns the remainder after dividing the first operands by the second operand.


2. Relational Operators

Relational operators are also known as comparison operators. A relational operator compares two operands and returns either a true or a false value. In other words, it helps to determine the relationship between two operands. The operands can either be numbers or string values.

Operator | Name | Description

  • == | Equal to | Returns true if both the operands are equal.
  • === | Identical | Returns true if both the operands are equal and are of the same. data type (introduced in PHP 4).
  • != | Not equal to | Returns true if the first operand is not equal to the second operand.
  • <> | Not equal to | Returns true if the first operand is not equal to the second operand.
  • !== | Not identical | Returns true if the first operand is not equal to the second operand or they are not of the same data type (introduced in PHP 4).
  • < | Less than | Returns true if the first operand is less than the second operand.
  • <= | Less than or equal to | Returns true if the first operand is less than or equal to the second operand.
  • > | Greater than | Returns true if the first operand is greater than the second operand.
  • >= | Greater than or equal to | Returns true if the first operand is greater than or equal to the second operand.


3. Logical Operators

Logical operators enable to combination of two or more expressions in a condition. The operands are first converted to Boolean values before they are compared. The logical operator then evaluates the expression and returns a Boolean value of either true or false.

Operator | Name | Description | General Form

  • AND / && | Logical AND operator | Returns true only if both the expressions are true | Expression1 AND Expression2 / Expression1 && Expression2

  • OR / | | | Logical OR operator | Returns true if only one of the expression is true | Expression1 OR Expression2 / Expression1 | | Expression2

  • XOR | Logical XOR operator | Returns true if either Expression 1 or Expression 2 is true, but not both | Expression1 XOR Expression2

  • ! | Logical NOT operator | Returns true only if the condition is not true | !Expression


4. Bitwise Operators

Bitwise operators operate on the bitwise representation of their operands. They are similar to logical operators and work on small-scale binary representations of data. If the operand is a string, it is performed only after converting it to its corresponding integer representation. if both the operands are strings, the bitwise operator will first convert them to their ASCIl values and operate between the corresponding character offsets of the two strings.

Operator | Name | Description | General Form

  • & | AND | Compares two bits set result to 1 if both bits are 1 and 0 otherwise | Operand1 & Operand2

  • | | OR | Compares two bits sets result to 1 if either of the bits is 1 and 0 otherwise | Operand1 | Operand2

  • ^ | EXCLUSIVE-OR | Compares two bits and sets the bit to 1 if bits are different and 0 otherwise | Operand1 ^ Operand2

  • ~ | COMPLEMENT | Compares and sets 0 bits to 1 and vice versa | ~Operand

  • <<< | SHIFT LEFT | Shifts bits of Operand1, Operand2 steps to left (each step means 'multiply by two') | Operand1 << Operand2

  • >> | SHIFT RIGHT | Shifts bits of Operand1, and Operand2 steps to the right (each step means 'divide by two') | Operand1 >> Operand2


5. Assignment Operators

Assignment operators enable the assignment of a value to a variable. The '=' sign is the assignment operator. It is different from the equal to '==' sign used in the relational operators. The syntax for an assignment statement is as follows:

expression 1 = 
expression 2;

The value of expression 2 is assigned to the variable in expression 1. 
The operand on the left side of the assignment operator '=' should be a variable. The assignment operation can be performed either by value or by reference. Assignment by value copies the value of expression 2 and assigns it to expression 1. Assignment by reference assigns a reference of expression 2 to expression 1. For example, the expression $a = $b assigns the value of $b to $a, and the expression $a = &$b sets $a to reference $bThe basic assignment operator can also be used as a combined operator in conjunction with arithmetic and string operators. The combined operators are known as shorthand operators.

The list of shorthand operations is as follows:

  • +=
  • -=
  • *=
  • %=
  • /=
  • .=


6. Increment and Decrement Operators

Increment and decrement operators operate only on variables. Apart from calculating the result value, these operators cause a variable to change its value as well. Increment operators increase the value of the operand by one. Decrement operators decrease the value of the operand by one. These are used chiefly within looping statements such as, for, do while, and while.


7. String Operators

String operators operate only on character data and any non-string operand is first converted before the operation is executed.

Operator | Name | Description

  • . | Concatenation | Returns a concatenated string
  • .= | concatenating assignment | Appends argument on the right of the operators to arguments on the left


8. Conditional or ternary Operators

A conditional operator is an alternative to the if-else statement. It evaluates an expression for a true or false value and then executes one of the two statements depending on the result of the evaluation. The syntax for the conditional operator is as follows:

$var1 = ($var2 = value1)? expr1: expr2

The operator
 evaluates the $var1 and if it is true, expr1 is evaluated and if it is false, expr2 is evaluated. To display the use of a conditional operator.


PHP Operators PHP operators PHP arithmetic operators PHP comparison operators PHP logical operators PHP assignment operators PHP bitwise operators PHP ternary operator PHP increment and decrement operators PHP string operators PHP array operators PHP t

advertisement