Operators in PHP

Source: Internet
Author: User
Tags arithmetic operators bitwise 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 Results
$A & $B and (Bitwise AND) A bit of 1 in $A and $B will be set to 1
$A | $B or (bitwise OR) A bit of 1 will be set to 1 for any one in $A and $b
$A ^ $B XOR (Bitwise XOR) A bit of $A and $B will be set to 1 for 1 and another for 0.
~ $A Not (bitwise negate) Set the bit to 0 in $A to 1 and vice versa
$A << $B Shift left Move a bit in $A to the left by $B (each move represents "multiplied by 2")
$A >> $B Shift Right (move to left) Move a bit in $A to the right by $B (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 Results
$A = = $b Equals TRUE if the type is converted after $a equals $b
$A = = = $b Congruent TRUE if $a equals $B, and they are of the same type
$A! = $b Range TRUE if the type is converted after $A is not equal to $b
$A <> $B Range TRUE if the type is converted after $A is not equal to $b
$A!== $B Not congruent TRUE if $A is 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.

<?phpvar_dump(0 == "a"); // 0 == 0 -> truevar_dump("1" == "01"); // 1 == 1 -> truevar_dump("10" == "1e1"); // 10 == 10 -> truevar_dump(100 == "1e2"); // 100 == 100 -> 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 Results
$A and $b and (Logical AND) True if $A and $B are true
$A or $b or (logical OR) True if $A or $B is any of the true
$A xor $B Xor (Logical XOR) True if $A or $B is either true, but not both
! $A not (logical non) True if $A is not true
$A && $B and (Logical AND) True if $A and $B are true
$A | | $B or (logical OR) True if $A or $B is any of the 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 $ A and $ b have the same key/value pair, True
Example name Results
$ A + $ b Union $ A and $ B Union
$ A = = $ b equal
$ A = = = $< /span>b congruent if $ A and $ b have the same key/value pairs and the order and type are the same, TR UE
$ A! = $ b unequal if $ A is not equal to $ B is TRUE
$ a <> $ b $ A is not equal to $ B is TRUE
$a!== $ b not congruent if $ A is not all equal to $ B then T RUE


    • 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.

Operators in PHP

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.