PHP operators for front-end learning and PHP operators for learning

Source: Internet
Author: User
Tags bitwise operators

PHP operators for front-end learning and PHP operators for learning
* Directory [1] summary [2] Arithmetic Operators [3] assignment operators [4] bit operators [5] comparison operators [6] Error Control [7] logical operators [8] strings connect to the front of the [9] array Operator

An operator is something that can generate another expression by giving one or more expressions. Similar to javascript, php also has similar operator syntax. This article will introduce php operators in detail.

[Note] the javascript operator syntax is now

 

Summary

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, for example, the familiar Arithmetic Operators + (plus) and-(minus). Most PHP operators use this type. What is the unique ternary operator? :, Three values are acceptable. It is generally called a "ternary operator"

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 ("+. If 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. on the other hand, "=" is right join, so $ a = $ B = $ c is equivalent to $ a = ($ B = $ c)

[Note] operators with the same priority that are not combined cannot be used together. For example, 1 <2> 1 is invalid in PHP. However, expression 1 <= 1 = 1 is legal because the priority of = is lower

Priority

Additional information of the combined direction operator does not include clone new clone and new left [array () Right ++ --~ Type and increment/decrease do not have instanceof type right! Logical operators left */% Arithmetic Operators left +-. Arithmetic Operators and string operators left <> bit operators none <= >>= comparison operators none =! ===! ==<><=> Comparison operator left & bit operator and reference left ^ bit operator left | bit operator left & logical operator left | logical operator left ?? Comparison operator left? : Ternary right = + =-= * = ** =/=. ===<=>= value assignment operator left and logical operator left xor logical operator left or logical operator

 

Arithmetic Operators
-$ A: returns the negative value of $. $ A + $ B addition: sum of $ a and $ B. The difference between $ a-$ B subtraction $ a and $ B. The product of $ a * $ B multiplication $ a and $ B. $ A/$ B Division $ a divided by the operator of $ B. $ A % $ B modulo $ a divided by the remainder of $ B

The Division operator always returns a floating point number. 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.

The operands of the modulo operator are converted to Integers (excluding decimal digits) before the operation)

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

<?phpecho (5 % 3)."\n";           // prints 2echo (5 % -3)."\n";          // prints 2echo (-5 % 3)."\n";          // prints -2echo (-5 % -3)."\n";         // prints -2?>

 

Value assignment operator

The basic value assignment operator is "=", which actually means that the value of the right expression is assigned to the number of operations on the left.

The value of the value assignment expression is the value assigned. That is, the value of "$ a = 3" is 3.

<? Php $ a = ($ B = 4) + 5; // $ a is now 9, while $ B is 4.?>

In addition to the basic value assignment operators, there are also "combined operators" suitable for all binary arithmetic, array sets, and string operators ", in this way, you can use its value in an expression and assign the result of the expression to it.

x = yx += yx -= yx *= yx /= yx %= y
<?php$a = 3;$a += 5; // $a = $a + 5;$b = "Hello ";$b .= "There!"; //$b = $b . "There!";?>

Reference Assignment

PHP supports Reference Assignment. Referencing assignment means that two variables point to the same data without copying anything.

<? Php $ a = 3; $ B = & $ a; // $ B is the reference print "$ a \ n" for $ "; // output 3 print "$ B \ n"; // output 3 $ a = 4; // modify $ aprint "$ a \ n "; // output 4 print "$ B \ n"; // also output 4. Is it changed because $ B is a reference of $ a?>

Increment/decrease

Example name effect + $ a plus the value of $ a, then $ a ++ is returned and $ a is returned, then, subtract the value of $ a from the value of $ a plus one -- $ a minus one, then return $ a -- and then return $ a. Then, subtract the value of $ a by one.
<? Php $ x = 10; echo ++ $ x; // output 11 $ y = 10; echo $ y ++; // output 10 $ z = 5; echo -- $ z; // output 4 $ I = 5; echo $ I --; // output 5?>

 

Bitwise operators

Bitwise operators allow you to evaluate and operate the specified bit in an integer.

The result of the example name $ a & $ B And (bitwise And) will set the bitwise of $ a And $ B to 1 $ a | $ B Or (bitwise Or) the bitwise of $ a and $ B is set to 1 $ a ^ $ B Xor (bitwise OR) set one of $ a and $ B as 1 and the other as 0 to 1 ~ $ A Not (bitwise inversion) sets the bitwise 0 in $ a to 1, and vice versa $ a <$ B Shift left (left Shift) move the bits in $ a to the left for $ B (each movement represents "multiplied by 2") $ a >>$ B Shift right (shifted to the right) move the bits in $ a to the right $ B (each movement indicates "divided by 2 ")

 

Comparison Operators

Comparison operators, as their names imply, allow comparison of two values

Example name result $ a = $ B = TRUE. If $ a equals $ B $ a = $ B after type conversion is TRUE, if $ a equals $ B, and their types are the same $! = $ B is not equal to TRUE. If $ a is not equal to $ B $ a after type conversion, $ a is not equal to $ B $ a after type conversion! = $ B is not all equal to TRUE. If $ a is not equal to $ B, or their types are different, $ a <$ B is smaller than TRUE, if $ a is strictly less than $ B $ a> $ B is greater than TRUE, if $ a is strictly greater than $ B $ a <= $ B is less than or equal to TRUE, if $ a is less than or equal to $ B $ a >=$ B is greater than or equal to TRUE, if $ a is greater than or equal to $ B

If you compare a number with a string or a string that involves the content of a number, the string is converted to a value and compared to a value. This rule also applies to switch statements. When = or! = During comparison, no type conversion is performed because the types and values must be compared.

<? 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"; // outputs 0 break; case "a": echo ""; break;}?>

Compare multiple types

Operation Number 1 Operation Number 2 type result null or string convert NULL to "", compare the number or vocabulary bool or null any other type to bool, FALSE <TRUEobject built-in class can define your own comparison. Different classes cannot compare string, resource, number string, resource, and number to convert strings and resources into numbers, compared with ordinary mathematics, array has a smaller number of Members. If the key in operation 1 does not exist in operation 2, the array cannot be compared, otherwise, the one-by-one value is greater than that of any other object types. array: Any other types of arrays are always larger.

Ternary Operators

"? : "A Ternary operator is a comparison operator. For an expression (expr1 )? (Expr2) :( expr3). If the value of expr1 is true, the value of this expression is expr2; otherwise, it is expr3.

<? Php $ a = 78; // score $ B = $ a >=60? "Pass": "fail"; echo $ B; // pass?>

 

Error Control Operator

PHP supports an error control operator :@. When it is placed before a PHP expression, any error information that may be generated by this expression is ignored.

[Note] the error control prefix @ operator is only valid for the expression. @ does not shield information that is incorrectly parsed. It cannot be placed before the definition of a function or class, it cannot be used for condition structures such as if and foreach.

<? Php $ a = 1; echo @ $ a; // 1 $ B; echo @ $ B; // No error?>

 

Logical operators
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 or) TRUE, if $ a or $ B is TRUE, but not at the same time! $ A Not (logical Not) TRUE. If $ 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
<? Php // foo () has no chance to be called at all. It is short-circuited by the operator $ a = (false & foo ()); $ B = (true | foo (); $ c = (false and foo (); $ d = (true or foo ()); // -------------------- // "|" higher than "or" // expression (false | true) the result is assigned to $ e // equivalent to: ($ e = (false | true) $ e = false | true; // Constant false is assigned to $ f, true is ignored // equivalent to :( ($ f = false) or true) $ f = false or true; var_dump ($ e, $ f); // bool (true) bool (false) // -------------------- // "&" is higher than "and ". // The result of the expression (true & false) is assigned to $ g // equivalent to :( $ g = (true & false) $ g = true & false; // constant true is assigned to $ h, false is ignored // equivalent to :( ($ h = true) and false) $ h = true and false; var_dump ($ g, $ h); // bool (false) bool (true)?>

 

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 to connect the value assignment operator (". ="), which attaches the right parameter to the parameter after the left

<?php$a = "Hello ";$b = $a . "World!"; // now $b contains "Hello World!"$a = "Hello ";$a .= "World!";     // now $a contains "Hello World!"?>

 

Array Operators
Example name result $ a + $ B Union $ a and $ B Union $ a ==$ B equal if $ a and $ B have the same key/value pair, TRUE $ a ===$ B. If $ a and $ B have the same key/value pairs and the order and type are the same, TRUE $! = $ B. If $ a is not equal to $ B, the value is TRUE $ a <> $ B. If $ a is not equal to $ B, the value is TRUE $! = $ B incomplete. If $ a is not complete, $ B is TRUE.
<?php$x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; var_dump($z);//array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" } echo "<br>";var_dump($x == $y);//bool(false)echo "<br>";var_dump($x === $y);//bool(false)echo "<br>";var_dump($x != $y);//bool(true)echo "<br>";var_dump($x <> $y);//bool(true)echo "<br>";var_dump($x !== $y);//bool(true)?> 

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.