Operator in PHP
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:
- Arithmetic operator
- Comparison operator
- Logical operator (or relational operator)
- Assignment operator
- 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 9Comparison 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% AConditional 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 YClassify 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 leftFollow tutorialspoint
Previous article: Constants in PHP
Next lesson: If, else, switch commands in PHP
You should read it
May be interested
- OpenAI Announces Operator, an AI Agent That Automatically Performs Tasks on User's Behalfthe company behind chatgpt is unveiling a new way to use ai. openai has just announced operator, an ai agent that can automatically perform tasks for you.
- EXCEPT operator in SQL Serverthe except operator in sql server is used to return the rows in the first select statement that are not returned in the second select statement.
- Wildcard representation operator in Accesswildcard, also known as a wildcard, can be used to determine the location of a specific item when you don't remember exactly how it was written.
- Operator in JavaScriptwe see the following simple expression: 4 + 5 is equal to 9. here 4 and 5 are operands and '+' called operator - operator.
- UNION operator in SQL Serverthis tutorial explains how to use the union operator in sql server with specific syntax and examples.
- Speedtest announces VNPT as Vietnam's No. 1 Internet network operatorduring the test, vnpt achieved download speed of 64mbps and upload speed of 65.07mbps.
- How to Drive an Excavatoran excavator is a large piece of machinery used to dig dirt and debris out of construction sites. using one on a construction site requires operator training and a state operator license.https://study.com/articles/become_an_excavator_edu...
- How to chat with Viettel operatorviettel customers can access viettel.vn website to chat directly with viettel operator in the fastest way and completely free instead of having to call the switchboard to get assistance. this new way of not only helps you save costs (no call charges) but also helps your phone to easily understand what you are asking.
- How to Become an Owner Operator Truck Driveran owner operator owns or leases a truck and hauls goods for client companies. if you want to start a trucking business, choose a niche market, such as logging or hazardous materials. project your costs and revenue to see how many...
- Load the stack of assignment operators in C ++you can overload the assignment operator (=) as you can with other operators in c ++ and it can be used to create an object like copying constructors.