PHP development basic tutorial operators

1. Arithmetic operations

Arithmetic operators are actually addition, subtraction, multiplication and division in elementary school:

5.png

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/>";
//综合运算
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

In mathematics, = (an equal sign) is called the assignment operator, that is: assign the value on the right side of the equal sign to the variable on the left side of the equal sign. The variable is the value on the right.

The code runs from top to bottom, so assignment can be assigned repeatedly from top to bottom:

Example:

<?php
//给变量赋不同的值,观察最后的输出结果
$x = 5;
$x = true;
$x = '爱你';
$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 (=)

6.png


3. Since Addition and self-subtraction operations

Self-addition and self-subtraction means adding 1 or subtracting 1.

7.png

Let’s understand the difference between

$x++ and ++$x in the above table. Example:

<?php
$x = 5;
$y = 5;
//先赋值后加:即先将$x的值赋值给$z。$x的值为5,所以将$x的值赋值给$z,$z也为5
//之后$x再自己加一
$z = $x++;
//先将$y自加1,$y等于5,自加1后结果为6。因此,$y的结果为6
//自加后,再将结果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:

8.pngLet’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

9.png

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

10.png

Example:

<?php
//$x二进制值为:0101
$x = 5;
//$y二进制值为:1000
$y = 8;
//0101与1000诸位进行与运算,两个都是1个则为1,其他都为0
//     0101
//     1000
//————————————
//结果 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

11.png

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:

12.png

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
//打开一个不存在的文件adfsafasdfasfasdfdsadf.txt,你运行一下会发现报错了。
//再前面再加上一个@符看看效果
$fp = fopen('adfsafasdfasfasdfdsadf.txt','r');

//fp = fopen('adfsafasdfasfasdfdsadf.txt','r');

?>

Output:

13.png

After adding @

<?php
//打开一个不存在的文件adfsafasdfasfasdfdsadf.txt,你运行一下会发现报错了。
//再前面再加上一个@符看看效果
//$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

Continuing Learning
||
<?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/>"; //综合运算 echo ($x+$y)*$x; echo "<br/>"; ?>
submitReset Code