PHP Learning Operators and operator precedence _php basics

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators
Arithmetic operators
Operator name Result
$a + $b addition $a and $b and
$a-$b subtraction $a and $b difference
$a * $b multiplication of $a and $b
$a/$b division $a divided by $b quotient
$a% $b modulo $a divided by the remainder of the $b
Increment/decrement operator
Operator name Result
+ + $a before adding a $a value plus one, then the operation
$a + + after adding $a value first operation, after adding a
--Reduce the value of the $a before $a, and then do the operation
$a-the value of the minus $a is operated first, minus one
Instance:
<?php
echo $a =5+ "5th"; Output: 10
Echo 10%3; Output: 1
echo 10+ $a + +; Output: 20
Echo 5--$a; Output:-5
?>
Comparison operators
Operator name Result
$a = = $b equal to TRUE if $a equals $b
$a = = = $b congruent TRUE if $a equals $b and their types are the same
$a!= $b Unequal TRUE If $a is not equal to $b
$a <> $b is not equal to TRUE if $a is not $b
$a!== $b not congruent TRUE, if $a is not equal to $b, or if they are of different types
$a < $b is less than TRUE if $a is strictly less than $b
$a > $b is greater than TRUE if $a strictly $b
$a <= $b less than or equal to TRUE if $a is less than or equal to $b
$a >= $b greater than or equal to TRUE if $a is greater than or equal to $b
Another conditional operator is "? : "(or ternary) operator.

Instance:
<?php
Var_dump (0== "a"); Output: bool (TRUE)
Var_dump (0== "00"); Output: bool (TRUE)
Var_dump (0=== "00"); Output: bool (FALSE)
Var_dump (0<> "abc"); Output: bool (FALSE)
Var_dump (0!== "01"); Output: bool (TRUE)
$a = 10;
$b = 20;
$str = $a > $b? ' True ': ' false ';
Echo $str; Output: false
?>
logical operators
Operator name Result
$a and $b logical and true if both $a and $b are true.
$a or $b logical or true if either $a or $b is true.
$a XOR $b exclusive or TRUE if $a and $b are not the same
! $a logic is not true if the $a is not true.
$a && $b logic and true if $a and $b are true.
$a | | $b logical OR True if either $a or $b is true.

Where and with &&, or with | | are two ways of writing the same logical operator.
Logical and logical or both are short-circuit operators. The PHP interpreter will not evaluate the right expression when the following logical expression is encountered:
<?php
$a = 10;
if (false && (+ + $a));
echo $a; Output: 10
$b = 10;
if (true or (+ + $b));
Echo $b; Output: 10
?>
Bitwise operators
Bitwise operators allow placement of the bits specified in the integer number. If both the left and right arguments are strings, the bitwise operator will manipulate the ASCII value of the character.
Expression name Result
The $a & $b is set to 1 for bits and 1 in the $a and $b.
$a | $b bitwise OR will set the bits of $a or $b 1 to 1.
$a ^ $b Bitwise XOR will set the $a and the different bits in the $b to 1.
~ $a bitwise NOT to set a bit of 0 in the $a to 1, or vice versa.
$a << $b move the bit left in the $a to the left $b times (each move is "multiplied by 2").
$a >> $b Move the bits in the $a to the right $b times (each move represents "divided by 2").
Other operators
String operators
There are two string operators. The first one is the concatenation operator ("." That returns the string after its left and right arguments are connected. The second is the connection assignment operator (". ="), which attaches the right argument to the left parameter.
Error suppression operator
In the most common database connections and file creation operations or when exceptions such as 0 occur, you can use the @ symbol to suppress function error information output to the browser end $a =@ (5/0)
External command execution
Use ' to run External system commands, note not single quotes, is the key below ESC
<?php
$out = ' dir c: ';
Print_r ($out);
? >//is not recommended for use
Instance:
<?php
$a = "Hello";
$a. = "world!  "; Equivalent to: $a = $a. "World!";
echo $a; Output: Hello world!
$m = 3;
$m + 5; Equivalent to: $m = $m +5;
Echo $m; Output: 8

$c = ($b = 4) + 5;
Echo $c; Output: 9
?>
Operator Precedence
The following table lists the precedence of operators from low to high.
Combining directional operators
Left
Left or
Left XOR
Left and
Right print
right = = = = *=/=. =%= &= |= ^= ~= <<=->>=
Left? :
Left | |
Left &&
Combining directional operators
Left |
Left ^
Left &
No = =!= = =!==
No < <= > >=
Left << >>
Left +-.
Left */%
Right! ~ + +-(int) (float) (string) (array) (object) @
Right
No new

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.