PHP base Operator

Source: Internet
Author: User
Tags arithmetic operators
1 definitions
An operator is something that can produce another value (and thus the entire structure as an expression) by giving one or more of the values (in programming jargon, expressions).

Operators can be grouped by their ability to accept several values:
The ① unary operator can accept only one value, such as! (logical inverse operator) or + + (increment operator).
The ② two-dollar operator accepts two values, such as the familiar arithmetic operator + (plus) and-(minus), which is the case for most PHP operators.
③ ternary operator? :, accepts three values; it is generally referred to as the "ternary operator" (although it might be more appropriate to call it a conditional operator).

Precedence of the 2 operator
The ① operator precedence specifies how tightly the two expressions are bound. For example, the result of an expression 1 + 5 * 3 is 16 instead of 18 because multiplication sign ("*") has a higher precedence than a plus sign ("+").
② you can use parentheses to force a change in precedence if necessary. For example: (1 + 5) * 3 has a value of 18.
③ If the operator has the same precedence, the union direction of the operator determines how it is to be calculated. For example, "-" is leftist, then 1-2-3 is equivalent to (1-2)-3 and the result is-4.
④ "=" is right-linked, so $a = $b = $c equals $a = ($b = $c).
⑤ no combination of the same precedence operators cannot be used together, such as 1 < 2 > 1 in PHP is not legal. But on the other hand the expression 1 <= 1 = = 1 is legal, because = = priority is lower than <=.
The use of ⑥ parentheses, even when not necessary, is clearly marked by the pairing of parentheses, rather than by operator precedence and binding, which can often increase the readability of the code.

3 Arithmetic operators
①:-$a represents the negative value of the $a.
② additions such as: $a + $b
③ subtraction: $a-$b
④ multiplication such as: $a * $b
⑤ division such as: $a/$b
⑥ modulus such as: $a% $b
The power of ⑦: $a * * $b

Attention:

A. The division operator always returns a floating-point number. The exception is the following: two operands are integers (or integers converted into strings) and exactly divisible, when it returns an integer.

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

C. The result of the modulo operator% is the same as the dividend symbol (sign). That is, the result of $a% $b is the same as the $a symbol

4 Assignment operators
① the basic assignment operator is "=". At first you might think it's "equal", not really. It actually means assigning the value of the right expression to the left operand.
The value of the assignment expression is the value assigned. In other words, the value of "$a = 3" is 3. This allows you to do some tips:

<?php    $a = ($b = 4) + 5;//$a now 9, and $b 4.    ?>

② Two-dollar arithmetic: the "combined operator" of an array collection and a string operator so that its value can be used in an expression and the result of the expression is assigned to it

<?php$a = 3; $a + = 5; Sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello"; $b. = "there!"; Sets $b to "Hello there!", just like $b = $b. "there!";? >

③ Reference Assignment: PHP supports reference assignment, using "$var = & $othervar;" Grammar. A reference assignment means that two variables point to the same data and nothing is copied.

<?php$a = 3; $b = & $a; $b is $a reference to print "$a \ n"; Output 3print "$b \ n"; Output 3$a = 4; Modify $aprint "$a \ n"; Output 4print "$b \ n"; Also output 4, because $b is a reference to $a and therefore is also changed?>

④ Common Sense
The assignment operation copies the value of the original variable into a new variable (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 (bitwise OR) $a | $b
③xor (Bitwise XOR) $a ^ $b
④not (bitwise reverse) ~ $a
⑤shift left (shift) $a << $b
⑥ $a >> $b

6 comparison operators
① equals $a = = $b
② congruent $a = = = $b
③ unequal $a! = $b
④ $a <> $b
⑤ not congruent $a!== $b
⑥ less than $a < $b
⑦ greater than $a > $b
⑧ is less than or equal to $a <= $b
⑨ is greater than or equal to $a >= $b
⑩ Combining comparison operators $a <=> $b

7 Error control operator
PHP supports an error control operator: @. Any error messages that may be generated by the expression are ignored until it is placed in a PHP expression.
If a custom error handler is set with Set_error_handler (), it will still be called, but this error handler can (and should) call Error_reporting (), which returns 0 if the function has @ at the top of the error statement.
If the track_errors attribute is activated, any error information generated by the expression is stored in the variable $php _errormsg. This variable is overwritten every time an error occurs, so check it out as soon as possible if you want to use it.

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

<?php$output = ' ls-al '; echo "<pre> $output </pre>";? >

Note: The anti-quote operator is not valid when Safe mode is activated or shell_exec () is turned off.

9 Increment/decrement operator: PHP supports C-style pre/post increment and decrement operators.
① Plus + + $a
② Plus $a + +
③ before--$a
④ $a after reduction--

10 logical operators
①and (logical AND) $a and $b
②or (logical OR) $a or $b
③xor (logical XOR) $a Xor $b
④not (logical non)! $a
⑤and (logic and) $a && $b
⑥or (logical OR) $a | | $b

11 String operators
There are two strings (string) operators. The first one is the join operator (".") ), which returns the string after its left and right arguments are concatenated. The second is the Join assignment operator (". ="), which attaches the right argument to the left argument. For more information, see assignment operators.

<?php$a = "Hello"; $b = $a. " World! "; Now $b contains "Hello world!" $a = "Hello"; $a. = "world!";     Now $a contains "Hello world!"? >

12 Array Operators
① Union of $a and $b. $a + $b
② equals True if $a and $b have the same key/value pair. $a = = $b
③ congruent if $a and $b have the same key/value pairs and the order and type are the same, TRUE. $a = = = $b
④ is TRUE if the $a does not equal $b. $a! = $b
⑤ is TRUE if the $a does not equal $b. $a <> $b
⑥ if the $a is not equal to $b is TRUE. $a!== $b

Note: The + operator appends the array element on the right to the left array, and the key names in all two arrays are ignored on the right side of the array.

13 Type Operators
Instanceof is used to determine whether a PHP variable belongs to an instance of a class class:

<?phpclass Myclass{}class notmyclass{} $a = new Myclass;var_dump ($a instanceof MyClass); Var_dump ($a instanceof Notmyclass);? >

The above is the PHP basic operator content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.