Basic PHP operators

Source: Internet
Author: User
Tags php operator
An operator can generate another value (thus the entire structure becomes an expression) by giving one or more values. 1 Definition
An operator can generate another value (thus the entire structure becomes an expression) by giving one or more values.

Operators can be grouped according to the values they can accept:
① The unary operator can only accept one value, for example! (Logical anti-operator) or ++ (incremental operator ).
② Binary operators can accept two values, such as the familiar arithmetic operators + (plus) and-(minus). most PHP operators use this type.
③ Ternary operator? :, Three values are acceptable. generally, it is called a "ternary operator" (although it is called a conditional operator may be more appropriate ).

2 operator priority
① The operator priority specifies how closely two expressions are bound ". For example, the result of expression 1 + 5*3 is 16 instead of 18 because the priority of the multiplication sign ("*") is higher than that of the plus sign ("+.
② When necessary, use parentheses to forcibly change the priority. Example: (1 + 5) the value of * 3 is 18.
③ If the operators have the same priority, the combination of operators determines how to calculate them. For example, if "-" is left join, 1-2-3 is equivalent to (1-2)-3 and the result is-4.
④ "=" Is right join, so $ a = $ B = $ c is equivalent to $ a = ($ B = $ c ).
⑤ An operator without the same priority cannot be used together. for example, 1 <2> 1 is invalid in PHP. However, expression 1 <= 1 = 1 is valid because the priority of = is lower than <=.
6. even if it is not necessary to use parentheses, the matching of parentheses is used to clearly indicate the operation sequence, rather than the operator priority and combination. this usually increases the readability of the code.

3 Arithmetic operators
① The inverse is as follows:-$ a indicates the negative value of $.
② Addition: $ a + $ B
③ Subtraction, for example, $ a-$ B
④ Multiplication: $ a * $ B
⑤ Division example: $ a/$ B
6. modulo: $ a % $ B
7. power: $ a ** $ B

Note:

A. division operators always return floating point numbers. Only in the following cases: both operands are integers (or integers converted into strings) and can be fully divided. in this case, an integer is returned.

B. The operand of the modulo operator is converted to an integer (excluding the decimal part) before the operation ).

C. The result of the modulo operator % is the same as that of the divisor (plus or minus sign. That is, the result of $ a % $ B is the same as that of $.

4 value assignment operator
① The basic assignment operator is "= ". At first, we may think it is "equal", but it is not. It actually means assigning the value of the right expression to the number of operations on the left.
The value of the value assignment expression is the value assigned. That is to say, the value of "$ a = 3" is 3. In this way, you can do some tips:

 
 

② Binary arithmetic: the "combined operator" of array sets and string operators. This allows you to use its value in an expression and assign the result of the expression to it.

 

③ Reference value assignment: PHP supports reference value assignment and uses the "$ var = & $ othervar;" syntax. Referencing a value assignment means that two variables point to the same data without copying anything.

 

④ Common sense
The value assignment operation copies the value of the original variable to the new variable (passing the value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in dense loops.

5-bit operators
① And (bitwise And) $ a & $ B
② Or (by bit Or) $ a | $ B
③ Xor (by bit or) $ a ^ $ B
④ Not (bitwise inversion )~ $
⑤ Shift left (left Shift) $ a <$ B
⑥ $ A >>> B

6 Comparison operators
① Equal to $ a = $ B
② Equi $ a ===$ B
③ Not equal to $! = $ B
④ Not $ a <> $ B
⑤ Incomplete wait $! ==$ B
⑥ Less than $ a <$ B
7. greater than $ a> $ B
Bandwidth less than or equal to $ a <= $ B
Bytes is greater than or equal to $ a >=$ B
Combined with the comparison operator $ a <=> $ B

7 Error control operators
PHP supports an error control operator :@. Before placing the expression in a PHP expression, any error information that may be generated by the expression is ignored.
If set_error_handler () is used to set a custom error processing function, it will still be called. However, this error processing function can (and should) call error_reporting (), this function returns 0 if @ is present before an error statement.
If the track_errors feature is activated, any error messages generated by the expression are stored in the variable $ php_errormsg. This variable will be overwritten every time an error occurs, so if you want to use it, you should check it as soon as possible.

8 execution operators
PHP supports an execution operator: backquotes (''). Note that this is not a single quotation mark! PHP will try to execute the content in the back quotes as a shell command and return its output information (that is, it can be assigned to a variable rather than simply discarded to the standard output ). The effect of using the unquoted operator "'" is the same as that of the shell_exec () function.

 $output
";?>

Note: The Ampersand operator is invalid when safe mode is activated or shell_exec () is disabled.

9 increment/decrease operator: PHP supports C-style increment and decrease operators.
① Add + + $
② Add $ a ++
③ Minus -- $
Minus $ --

10 logical operators
① And (logical and) $ a And $ B
② Or (logical or) $ a Or $ B
③ Xor (logical xor) $ a Xor $ B
④ Not (logical Not )! $
⑤ And (logical And) $ a & $ B
⑥ Or (logical Or) $ a | $ B

11 string operators
There are two string operators. The first is the concatenation operator ("."), which returns the string after the left and right parameters are connected. The second is the join value assignment operator (". ="), which attaches the right parameter to the parameter on the left. For more information, see the value assignment operator.

 

12 array operators
① Union of $ a and $ B. $ A + $ B
② Equal TRUE if $ a and $ B have the same key/value pairs. $ A = $ B
③ TRUE if $ a and $ B have the same key/value pairs and the order and type are the same. $ A ===$ B
④ If $ a is not equal to $ B, it is TRUE. $! = $ B
⑤ If $ a is not equal to $ B, it is TRUE. $ A <> $ B
⑥ Incomplete. if $ a is not all equal to $ B, TRUE is returned. $! ==$ B

Note: the + operator attaches the array element on the right to the back of the array on the left. if both arrays have key names, only the elements in the array on the left are ignored.

13 type operators
Instanceof is used to determine whether a PHP variable belongs to a class:

 

The above is the basic PHP operator content. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.