Operators in PHP

Source: Internet
Author: User
Tags bitwise operators
Arithmetic operators Operator description
+
-
*
/ 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.
% The operands of the modulo operator are converted to integers (minus fractional parts) before the operation. 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.


Echo (5 3). " \ n ";           Prints 2echo (5-3). " \ n ";          Prints 2echo (-5% 3). " \ n ";          Prints-2echo (-5%-3). " \ n ";         Prints-2
Assignment operators

The basic assignment operator is "="

In addition to the basic assignment operator, there are "combined operators" that are suitable for all two-element arithmetic, array collections, and string operators

$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!";
Bitwise operators Example name result
$a & $b and (Bitwise AND) Set the 1 bit in the $a and $b to 1
$a | $b or (bitwise OR) A bit of 1 will be set to 1 for any one of the $a and $b
$a ^ $b XOR (Bitwise XOR) A bit of 1 and a 0 in the $a and $b will be set to 1
~ $a Not (bitwise negate) Set the bit to 0 in the $a to 1 and vice versa
$a << $b Shift left Moves the bits in the $a to the left $b times (each move represents "multiplied by 2")
$a >> $b Shift Right (move to left) Moves the bits in the $a to the right $b times (each move represents "divided by 2")

The right side of the left shift is filled with 0, and the sign bit is removed, which means the signs are not retained. When you move right, the left side is filled with a symbol bit, which means the sign is retained.

Comparison operators Example name result
$a = = $b Equals TRUE if the type is converted after $a equals $b
$a = = = $b Congruent TRUE if $a equals $b, and their type is the same
$a! = $b Range TRUE if the $a after the type conversion is not equal to $b
$a <> $b Range TRUE if the $a after the type conversion is not equal to $b
$a!== $b Not congruent TRUE if $a are not equal to $b, or if they are of different types
$a < $b Small and TRUE if $a is strictly less than $b
$a > $b Greater than TRUE if $a is strictly greater than $b
$a <= $b Less than or equal TRUE if $a is less than or equal to $b
$a >= $b Greater than or equal TRUE if $a is greater than or equal to $b


If you compare a number and a string or compare strings that involve numeric content, the strings are converted to numeric values and compared to numeric values. This rule also applies to switch statements. The type conversion is not performed when compared with = = = or!==, because the type and the value are compared at this time.

 
  Truevar_dump ("1" = = "01"); 1 = = 1-truevar_dump ("ten" = = "1e1"); Ten = = Truevar_dump (+ = "1e2"); + = Trueswitch ("a") {case 0:    echo "0";    Break;case "a"://Never reached because "a" is already matched with 0    echo "a";    break;}? >

Ternary operators

Another conditional operator is the "?:" (or ternary) operator

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.

The @ operator is valid only for an expression. A simple rule for beginners is that if you can get a value from somewhere, you can precede it with the @ operator. For example, you can put it before variables, functions, and include calls, constants, and so on. It cannot be placed before the definition of a function or class, nor can it be used for conditional structures such as if and foreach

Execute operator

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 ().

Anti-quotes cannot be used in double-quote strings

Increment/decrement operator logical operator Example name result
$a and $b and (Logical AND) True if both $a and $b are true
$a or $b or (logical OR) True if either $a or $b is true
$a XOR $b Xor (Logical XOR) True if either $a or $b is true, but not both
! $a not (logical) True if the $a is not true
$a && $b and (Logical AND) True if both $a and $b are true
$a | | $b or (logical OR) True if either $a or $b is true


There are two different form operators for "and" and "or" because their operations have different precedence

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 one is the join assignment operator (". =")

Array operators Example name result
$a + $b Joint Union of $a and $b
$a = = $b Equal TRUE if $a and $b have the same key/value pair
$a = = = $b Congruent TRUE if $a and $b have the same key/value pairs and the order and type are the same
$a! = $b Range TRUE if $a is not equal to $b
$a <> $b Range TRUE if $a is not equal to $b
$a!== $b Not congruent TRUE if the $a is not all equal to $b


  • The operator appends the array element on the right to the left array, and the key names in each of the two arrays are ignored on the right side of the array.
  • Type operator

    Instanceof used to determine if a PHP variable belongs to an instance of a class

    Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

  • Related Article

    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.