Basic Introduction to operators in php. In php, our commonly used operators include arithmetic operators, value assignment operators, comparison operators, and logical operators. I will introduce the usage of these operators to you. Division of arithmetic operators in php our commonly used operators include arithmetic operators, value assignment operators, comparison operators, logical operators and so on. I will introduce the usage of arithmetic operators to you.
Arithmetic operators
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 ).
Note: the result of modulo $ a % $ B when $ a is a negative value is also a negative value.
Example:
The code is as follows: |
|
/* Tested under PHP 5.2.6-1 with Suhosin-Patch 0.9.6.2 (cli) on both i386 and amd64, Debian lenny/sid */ $ A = 2863311530; $ B = 256; $ C = $ a % $ B; Echo "$ c N "; Echo (2863311530% 256 )." N ";/* directly with no variables, just to be sure */ ?> |
Operator |
Description |
Example |
Result |
+ |
Addition |
X = 2 X + 2 |
4 |
- |
Subtraction |
X = 2 5-x |
3 |
* |
Multiplication |
X = 4 X * 5 |
20 |
/ |
Division |
15/5 5/2 |
3 2.5 |
% |
Modulus (pision remainder) |
5% 2 10% 8 10% 2 |
1 2 0 |
++ |
Increment |
X = 5 X ++ |
X = 6 |
-- |
Decrement |
X = 5 X -- |
X = 4 |
Value assignment operator
The basic assignment operator is "= ". At first, we may think it is "equal", but it is not. It actually means assigning the value of the right expression to the number of operations on the left.
The value of the value assignment expression is the value assigned. That is to say, the value of "$ a = 3" is 3. In this way, you can do some tips:
The code is as follows: |
|
$ 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. for example:
The code is as follows: |
|
$ 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! "; ?> |
Note that the value assignment operation copies the value of the original variable to the new variable (pass the value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in intensive loops. You can also assign values by reference using the $ var = & $ othervar; syntax. Referencing a value assignment means that both variables point to the same data without any data copies. For more information about references, see references. In PHP 5, objects are always assigned by reference unless the new clone keyword is explicitly used.
Operator |
Description |
Example |
= |
X = y |
X = y |
+ = |
X + = y |
X = x + y |
-= |
X-= y |
X = x-y |
* = |
X * = y |
X = x * y |
/= |
X/= y |
X = x/y |
. = |
X. = y |
X = x. y |
% = |
X % = y |
X = x % y |
Comparison Operators
Comparison operators, as their names imply, allow comparison of two values. If you compare an integer with a string, the string is converted to an integer. Compare two numeric strings as integers. This rule also applies to switch statements.
The code is as follows: |
|
Var_dump (0 = "a"); // 0 = 0-> true Var_dump ("1" = "01"); // 1 = 1-> true Var_dump ("1" = "1e0"); // 1 = 1-> true Switch (""){ Case 0: Echo "0 "; Break; Case "a": // never reached because "a" is already matched with 0 Echo ""; Break; } ?> |
Operator |
Description |
Example |
= |
Is equal |
5 = 8 returns false |
! = |
Is not equal |
5! = 8 returns true |
> |
Is greater |
5> 8 returns false |
< |
Is less |
5 <8 returns true |
> = |
Is greater than or equal |
5> = 8 returns false |
<= |
Is less than or equal |
5 <= 8 returns true |
Logical operators
"And" and "or" have two different operators because they have different operation priorities (see operator priorities ).
Example #1 Example of logical operators
The code is as follows: |
|
// The following foo () will not be called because they are short-circuited by the operator. $ A = (false & foo ()); $ B = (true | foo ()); $ C = (false and foo ()); $ D = (true or foo ()); // "|" Has a higher priority than "or" $ E = false | true; // $ e is assigned (false | true) and returns true. $ F = false or true; // $ f is assigned to false. [Altair note: "=" has a higher priority than "or"] Var_dump ($ e, $ f ); // "&" Has a higher priority than "and" $ G = true & false; // $ g is assigned to (true & false) and the result is false. $ H = true and false; // $ h is assigned true [Altair note: "=" priority is higher than "and"] Var_dump ($ g, $ h ); ?> The output of the preceding routine is similar: Bool (true) Bool (false) Bool (false) Bool (true) |
Operator |
Description |
Example |
&& |
And |
X = 6 Y = 3(X <10 & y> 1) returns true |
| |
Or |
X = 6 Y = 3(X = 5 | y = 5) returns false |
! |
Not |
X = 6 Y = 3! (X = y) returns true |
Let me introduce the usage to all of you. Arithmetic operator division...