PHP development basic tutorial operators
1. Arithmetic operations
Arithmetic operators are actually addition, subtraction, multiplication and division in elementary school:
Example:
<?php //聲明變量 $x = 16; $y = 5; //加 echo $x+$y; echo "<br/>"; //減 echo $x-$y; echo "<br/>"; //乘 echo $x*$y; echo "<br/>"; //除 echo $x/$y; echo "<br/>"; //取余 echo $x%$y; echo "<br/>"; //綜合運(yùn)算 echo ($x+$y)*$x; echo "<br/>"; ?>
Note: As we learned in mathematics, there is also a priority: multiplication and division first, addition and subtraction later. If you want to change the priority more explicitly, use () [parentheses] to enclose the value you want to take precedence
## 2. Assignment operation
<?php //給變量賦不同的值,觀察最后的輸出結(jié)果 $x = 5; $x = true; $x = '愛(ài)你'; $x = 12.888; echo $x; ?>It can be seen that $x is assigned repeatedly , subsequent assignments will overwrite previously assigned values. The output is the last assignment. PHP has several other extended assignments, all of which evolved from assignment (=)
3. Since Addition and self-subtraction operations
Self-addition and self-subtraction means adding 1 or subtracting 1.<?php $x = 5; $y = 5; //先賦值后加:即先將$x的值賦值給$z。$x的值為5,所以將$x的值賦值給$z,$z也為5 //之后$x再自己加一 $z = $x++; //先將$y自加1,$y等于5,自加1后結(jié)果為6。因此,$y的結(jié)果為6 //自加后,再將結(jié)果6偷偷的賦值給自己$w $w = ++$y; echo 'x的值為'.$x; echo "<br/>"; echo 'y的值為'.$y; echo "<br/>"; echo 'z的值為'.$z; echo "<br/>"; echo 'w的值為'.$w; echo "<br/>"; ?>
Note : You can try the difference between $x-- and --$x
## 4. Comparison operations
PHP’s comparison operator can compare two values. After comparison, the Boolean value true or false is returned:
Let’s take a look at the example
Example:
<?php $x=2; $y=4; var_dump($x>$y); echo "<br/>"; var_dump($x<$y); ?>
The above are all common and can easily cause confusion in understanding. There are two == and ===
== equal to
== = are all equal, also called judging type equals
Let’s take a look at an example:
Instance
<?php $x=3; $y="3"; var_dump($x==$y); echo "<br/>"; var_dump($x===$y); ?>
From the above example, we can see that == compares the left and right Whether the values ??of numbers are equal, === is more strict. It not only compares whether the values ??are equal, but also compares whether the types are equal.
Note:! = (not equal to) and! The same is true for == (all not equal). You can try to output and see
5. Logical operations
Logical operators are relatively simple. A way for humans to think logically
Assume that $x is condition one and $y is condition two
## Logical AND: The Chinese explanation is and, that is, when $ When x and $y are both true (true), it returns true (true). In other cases, it returns false (false)- Logical OR: Chinese interpretation is or. That is, when one of $x or $y is true (true), return true (true), and when both are false (false), return false (false)
Logical negation: Chinese explanation is negation. If $x is false, perform a non-operation. If it is not false (false), it is true, and it can return true (true). If true is reversed, false will be returned.
Logical XOR: If $x and $y are the same, it is false, if they are not the same, it is true
See the table below for details
Let’s take a look at an example:
<?php $x=1; $y=0; var_dump($x&&$y); echo "<br/>"; var_dump($x||$y); echo "<br/>"; var_dump(!$x); echo "<br/>"; var_dump($x xor $y); echo "<br/>"; ?>
6. Bit operations
The bit operators are based on binary For you to make a logical comparison
Example:
<?php //$x二進(jìn)制值為:0101 $x = 5; //$y二進(jìn)制值為:1000 $y = 8; //0101與1000諸位進(jìn)行與運(yùn)算,兩個(gè)都是1個(gè)則為1,其他都為0 // 0101 // 1000 //———————————— //結(jié)果 0000 var_dump($x & $y); ?>
Note: Only one example is listed. If you are interested in others, you can make your own Try the output and see if the result is the same as you expected
7. Operator precedence
The learning level of this chapter is understanding level.
Because most people don’t remember the precedence of operators.
When we were in elementary school, the priority was multiplication and division first, then addition and subtraction. If you want to change the priority of an operation, just add parentheses.
Key point: You don’t need to remember the priority. When you are unsure, just mark the priority in brackets
Note: If you are interested, you can read the PHP manual. There is a detailed priority description above
8. Ternary operator and other operators
There are also some special operators and symbols, which we will explain later. Maybe we need to use
in the future. 1. The ternary operator
is equivalent to the if we will learn in the next chapter. .else structure. However, the ternary operator is written more concisely. The syntax format is as follows:
Judge whether $x is true? The code segment executed if it is true (you can only write one line of code): The code segment executed if it is false (you can only write one sentence) Write a code);
Example:
<?php $x = true; $x ? $y = 5 : $y = 6; //輸出5 echo $y; ?>
2. Backticks
We often need to display the IP address, but it is impossible to display it in PHP What about the Windows IP address? Using backticks, we can execute our commands (but some virtual servers prohibit the execution of these command scripts):
Source code:
<?php echo '<pre>'; echo `ipconfig`; echo '</pre>'; ?>
Output result:
Note: The backtick is in the upper left corner of the keyboard, above the Tab key
3.@ symbol
Single line suppression of errors, future chapters We will also explain the
example. The source code is as follows:
<?php //打開(kāi)一個(gè)不存在的文件adfsafasdfasfasdfdsadf.txt,你運(yùn)行一下會(huì)發(fā)現(xiàn)報(bào)錯(cuò)了。 //再前面再加上一個(gè)@符看看效果 $fp = fopen('adfsafasdfasfasdfdsadf.txt','r'); //fp = fopen('adfsafasdfasfasdfdsadf.txt','r'); ?>
Output:
After adding @
<?php //打開(kāi)一個(gè)不存在的文件adfsafasdfasfasdfdsadf.txt,你運(yùn)行一下會(huì)發(fā)現(xiàn)報(bào)錯(cuò)了。 //再前面再加上一個(gè)@符看看效果 //$fp = fopen('adfsafasdfasfasdfdsadf.txt','r'); @$fp = fopen('adfsafasdfasfasdfdsadf.txt','r'); ?>
There is no output and no warning. It can be seen that the @ symbol suppresses the error