What is the operator? The simple answer from expression 4 + 5 is equal to 9. Here, 4 and 5 are called operands and + are called operators. PHP language supports the following operator types:

  1. Arithmetic operator
  2. Comparison operator
  3. Logical operator (or relational operator)
  4. Assignment operator
  5. Conditional operator (or 3-star operator)

Arithmetic operators in PHP

The table below lists the arithmetic operators supported by the PHP language:

Suppose variable A holds value 10, variable B holds 20 then:

For example:

Operator Description Example + Add two operands A + B with the result 30 - Subtract the second operand from the first operand A - B resulting in -10 * Multiply the two operands A * B resulting in 200 / Division B / A result is 2% The balance of B% A result is 0 ++ The operator increases, adds the operand value an A ++ unit, the result is 11 - The operator decreases, decreases the value of the operand A unit A - the result is 9

Comparison operators in PHP

The table below lists the comparison operators supported by the PHP language. Suppose variable A holds value 10, variable B holds value 20, then:

For example:

Operator Description Example == Check if 2 operands are equal or not. If equal, the condition is true. (A == B) is not true. ! = Check if two operands have different values. If not, the condition is true. (A! = B) is true. > Check if the left operand is greater than the right operand. If larger, the condition is true. (A> B) is not true. If it is smaller then true. (A = Check if the left operand has a value greater than or equal to the value of the right operand. If true, true. (A> = B) is not true. <= Check if the left operand is less than or equal to the right operand. If true, true. (A <= B) is true.

Logical operators in PHP

The table below shows all logical operators supported by the PHP language. Suppose variable A has value 10 and variable B has value 20, then:

For example:

Operator Descriptionand Example Called Logic AND operator. If both operands are true, the condition becomes true (A and B) is true. or Called Logic OR operator. If one of the operands is true, the condition becomes true (A or B) is true. && Called the Logic AND operator. If both operands are true, the condition becomes true (A && B) is true. || Called Logic OR operator. If one of the operands is true, the condition becomes true (A || B) is true. ! Called Logic NOT operator. Use to reverse the logic state of operands. If the condition is true, the Logic NOT operator will result in false! (A && B) is false.

The assignment operator in PHP

Here are the assignment operators supported by the PHP language:

For example:

Description OperatorExample = Simple assignment operator. Assign the right operand value to the left operand C = A + B to assign the value of A + B to C + = Add the operand value to the left operand and assign that value to the left operand C + = A is equal to C = C + A - = Subtract the operand value from the left operand and assign this value to the left operand C - = A is equivalent to C = C - A * = Multiply the price value the operand with the left operand and assign this value to the left operand C * = A which is equivalent to C = C * A / = Divide the left operand for the right operand and assign this value to the left operand C / = A is equal to C = C / A% = Taking the remainder of the left operand division for the right operand and assigning the left operand C% = A is equivalent to C = C% A

Conditional operator in PHP

There is more than one operator called conditional operator. First, it evaluates an expression to true or false and then executes either of the given commands depending on the outcome of the estimate. The conditional operator has the following syntax:

For example:

Operator Description For example? : Conditional expression If the condition is true? Then the value X: If not, the value Y

Classify operators in PHP

All of the above operators can be classified into the following types in PHP:

Unary operator, which precedes an operand.

The binary operator, receives two operands and performs various arithmetic and logical operations.

The ternary operator or the trin operator, receives three operands and estimates or the second or the third expression, depending on the estimated results of the first expression.

The assignment operator, which assigns a value to a variable.

Operator priority in PHP

Operator priority in PHP determines how the expression is calculated. For example, the multiplication operator takes precedence over the addition operator, and it is done first.

For example, x = 7 + 3 * 2; Here, x is assigned a value of 13, not 20 because the * operator has a higher priority than the + operator, so first it performs multiplication 3 * 2 and then adds 7.

The following table lists the priority order of operators in PHP. Operators with the highest priority appear at the top of the table, and the operators with the lowest priority are at the bottom of the table. In an expression, the highest priority operators are first calculated.

Type of deathUnited priority order! ++ - Right to left Multiplication * /% Left to right Addition + - Left to right Relationship <<=>> = Left to right Calculated = =! = Left to right Logic AND && Left to right Logic OR || Left to right Condition: Right to left Assign = + = - = * = / =% = Right to left

Follow tutorialspoint

Previous article: Constants in PHP

Next lesson: If, else, switch commands in PHP

5 ★ | 1 Vote | 👨 227 Views

Above is an article about: "Operator in PHP". Hope this article is useful to you. Don't forget to rate the article, like and share this article with your friends and relatives. Good luck!

« PREV POST
NEXT POST »