Home > Backend Development > PHP Tutorial > PHP basic tutorial three operators

PHP basic tutorial three operators

黄舟
Release: 2023-03-06 08:44:02
Original
1381 people have browsed it

Content explained in this section

  • Arithmetic operators

  • Increment and decrement operators

  • Assignment operator

  • String operator

  • ##Comparison operator

  • Bitwise operators

  • Logical operators

  • Ternary operators

  • Error control operator

Preface

PHP operators are a skill that must be mastered in the basics of PHP , it is a bridge for PHP data processing. Operators are used in various operations on data, such as addition, subtraction, multiplication, division, and, or and other operations.

Arithmetic operator

Arithmetic operator: It is the symbol used to process the four arithmetic operations. This is the simplest and most commonly used symbol, especially for the processing of numbers. , arithmetic operation symbols are almost always used.

Arithmetic operator classification:

-$a negation, negative value of $a

$a = -1;
echo -$a;
-----结果-----
1
Copy after login

$a + $b addition sum of $a and $b

$a = 12;
$b = 12;
$res = $a + $b;
echo $res;
-----结果-----
24
Copy after login

$a - $b subtraction the difference between $a and $b

$a = 12;
$b = 6;
$res = $a - $b;
echo $res;
-----结果-----
6
Copy after login

$a * $b multiplication the product of $a and $b

$a = 2;
$b = 3;
$res = $a * $b;
echo $res;
-----结果-----
6
Copy after login

$a / $ b The quotient of division $a and $b

$a = 12;
$b = 6;
$res = $a / $b;
echo $res;
-----结果-----
2
Copy after login

$a % $b modulo the remainder of $a and $b;

$a = 3;
$b = 2;
$res = $a % $b;
echo $res;
-----结果-----
1
Copy after login

Increment and decrement operators

  • ++$a: The value of $a is increased by one, and then $a is returned. This is pre-increment.

  • $a++ returns $a and then increments the value of $a by one, which is post-increment.

  • –$a Decrement the value of $a by one, then return $a , decremented before.

  • $a– Returns $a, then decrements the value of $a by one, and then decrements.


++$a

$a = 1;
$b = ++$a;//先++,在进行赋值运算,等价于$a = $a + 1; $b = $a;
echo $b . &#39;<br>&#39; . $a;
-----结果-----
$b = 2 $a = 2
Copy after login

$a++

$a = 1;
$b = $a++;//先赋值,在进行++运算,等价于$b = $a; $a = $a + 1;
echo $b . &#39;<br>&#39; . $a;
-----结果-----
$b = 1  $a = 2
Copy after login

Less The reduction situation is the same as above.

Assignment operator

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

$a = 1;
$b = &#39;string&#39;;
Copy after login

There are also cases where

+=, -=, /=, etc.

$a = 1;
$a += 2;//等价于$a = $a + 2;
echo $a;
-----结果-----
3
Copy after login

string Operator

has two strings. The operator is ("."), which returns a string concatenated with its left and right arguments.

$a = 1;
$b = 2;
echo &#39;$a = &#39; . $a . &#39;  $b = &#39; . $b;
-----结果-----
$a = 1 $b = 2;
Copy after login

Comparison operators

Comparison operators are used to compare two variables, whether the values ​​​​are the same and whether the data types are the same, and most of the comparison results are Boolean type.

Common comparison operators are:

  • $a == $b is equal to true. When $a == $b, here we will generally only judge whether the numerical values ​​are equal. , will not determine whether the types are equal.

  • $a === $b Congruent true if $a is equal to $b and they are of the same type.

  • $a != $b is not equal to true, returns true when the values ​​of the two variables are not equal;

  • $a < ;> $b is not equal to true. There is another way to write the above situation, if $a is not equal to $b after type conversion.

  • $a !== $b is not equal to true. If $a is not equal to $b, or their types are different, note that when one of the two variables is not equal, true will be returned. ;(Number, type)

  • $a < $b is less 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 .

    $a = 2;
    $b = &#39;2&#39;;
    var_dump($a == $b); //判断两个数值是否相等,比较的时候会进行类型转换。
    -----结果-----
    bool(true)  
    var_dump($a === $b);//判断数值和类型是否相等,当时全等比较的时候,不会进行类型转换。
    -----结果-----
    bool(false)
    Copy after login

Bit operators

The bit operators allow you to specify bits in an integer number in

binary to evaluate and operate.

Before that, let’s talk about binary first

A brief introduction to binary

In the computer, all numbers stored in the memory are in the form of Stored in binary form. To put it bluntly, bit operations are to directly operate on the binary bits of integers in memory.

What is binary: Binary data is a number represented by two digits: 0 and 1. Its base is 2, the carry rule is "when two are entered, one is added", and the borrowing rule is "borrow one to be equal to two".

Convert decimal to binary: "Divide by 2 and take the remainder, arrange in reverse order" (divide by two and take the remainder)

        商   余数
3 / 2 = 1.....1
1 / 2 = 0.....1
二进制(逆序排列):
11

12 / 2 = 6....0
6 / 2 = 3.....0
3 / 2 = 1.....1
1 / 2 = 0.....1
二进制:
1100

15 / 2 = 7....1
7 / 2 = 3.....1
3 / 2 = 1.....1
1 / 2 = 0.....1
二进制:
1111
Copy after login

Convert binary to decimal: "Expand sum by weight"

1111 = 1 * 20 + 1 * 21 + 1 * 22 + 1 * 23 = 15
10 = 0 * 20 + 1 * 21 = 2
Copy after login

The symbol bit in the computer has three expression forms:

  • Original code

  • Inverse code

  • Complement code

When performing bit operations, they are converted into complement codes for operations. of.

The binary representation of a positive number is that there is a 0 at the front of the binary number to indicate a positive numberThe original code of the positive number, the inverse code, and the complement code are the same (three codes in one)

The binary representation of a negative number is that there is a 1 at the beginning of the binary to represent a negative number. The original code of a negative number is a binary number obtained by using a formula. The complement code means that except the sign bit, everything else is inverted (1 becomes 0, 0 becomes 1), and the complement code is the complement code + 1.

For example -1

原码 10000000 00000000 00000000 00000001 
反码 11111111 11111111 11111111 11111110 取反
补码 11111111 11111111 11111111 11111111 加1
Copy after login

As for why it is necessary to write 32 bits, my understanding is: the integer occupies 4 bytes, each byte is 8 bits, a total of 32 bits, one of which is a symbol Bit.

This is the brief introduction to binary system


Classification of bitwise operators in PHP

  • $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。

  • $a | $b Or(按位或) 将把 $a 和 $b 中任何一个为 1 的位设为 1。

  • $a ^ $b Xor(按位异或) 将把 $a 和 $b 中一个为 1 另一个为 0 的位设为 1。

  • ~ $a Not(按位取反) 将 $a 中为 0 的位设为 1,反之亦然。

  • $a << $b Shift left(左移) 将 $a 中的位向左移动 $b 次(每一次移动都表示“乘以 2”)。

  • $a >> $b Shift right(右移) 将 $a 中的位向右移动 $b 次(每一次移动都表示“除以 2”)。

示例:

与(1 1 = 1; 1 0 = 0;0 0 = 0)

$a = 1 & 2;//进行“与”运算  两个都为1的时候为1,其他为0
// 1的补码
// 00000000 00000000 00000000 00000001
// 2的补码
// 00000000 00000000 00000000 00000010
// 结果
// 00000000 00000000 00000000 00000000 == 0
echo $a;
-----结果-----
0
Copy after login

或(1 1 = 1;1 0 = 1;0 0 = 0)

$a = 1 | 2;//进行“或”运算  至少有一个为1的时候为1,其他为0
// 1的补码
// 00000000 00000000 00000000 00000001
// 2的补码
// 00000000 00000000 00000000 00000010
// 结果
// 00000000 00000000 00000000 00000011 == 3
echo $a;
-----结果-----
3
Copy after login

异或(1 0 = 1; 1 1 = 0; 0 0 = 1)

$a = 3 ^ 2;//进行“异或”运算  一个为1,一个为0的时候为1,其他为0
// 1的补码
// 00000000 00000000 00000000 00000011
// 2的补码
// 00000000 00000000 00000000 00000010
// 结果
// 00000000 00000000 00000000 00000001 == 1
echo $a;
-----结果-----
1
Copy after login

取反(1 = 0; 0 = 1)注意符号位的变化。

$a = ~2;//进行“取反”运算  1变0,0变1
// 2的二进制
// 00000000 00000000 00000000 00000010
// 11111111 11111111 11111111 11111101 前面一个1,是负数
// 反码
// 11111111 11111111 11111111 11111100
// 原码
// 10000000 00000000 00000000 00000011 == -3
echo $a;
-----结果-----
-3
Copy after login

左移(将 $a 中的位向左移动 $b 次(每一次移动都表示“乘以 2”))

左移时右侧以零填充,符号位被移走意味着正负号不被保留。

$a = 1;
// 二进制:
// 00000000 00000000 00000000 00000001
// 左移时右侧以零填充,符号位被移走意味着正负号不被保留。
// 00000000 00000000 00000000 00000100 == 4
echo $a << 2;//将 $a 中的位向左移动2次
-----结果-----
4
Copy after login

右移时左侧以符号位填充,意味着正负号被保留。

$a = 4;
// 右移时左侧以符号位填充,意味着正负号被保留。 
// 00000000 00000000 00000000 00000100
// 00000000 00000000 00000000 00000001 == 1
echo $a >> 2;
-----结果-----
1
Copy after login

到这里位运算符算术基本说完了……

逻辑运算符

逻辑运算符也就是判断逻辑,它的返回结果一般是布尔类型,像与或非等;而PHP中的逻辑运算符有:

  • $a and $b And(逻辑与) TRUE ,如果 $a 和 $b 都为 TRUE 。

  • $a or $b Or(逻辑或) TRUE ,如果 $a 或 $b 任一为 TRUE 。

  • ! $a Not(逻辑非) TRUE ,如果 $a 不为 TRUE 。

  • $a && $b And(逻辑与) TRUE ,如果 $a 和 $b 都为 TRUE 。

  • $a || $b Or(逻辑或) TRUE ,如果 $a 或 $b 任一为 TRUE 。

逻辑与(当表达式的两边都为true的时候结果为真)

两个&符号:

$a = 2;
$b = 3;

if($a > 1 && $b > 2){ //左边的为true,右边的为true;两边都为true,结果为true;
    echo &#39;与&#39;;
}
-----结果-----
与
Copy after login

一个&符号:

$a = 2;
$b = 3;

if($a > 1 & $b > 2){ //左边的为true,右边的为true;两边都为true,结果为true;
    echo &#39;与&#39;;
}
-----结果-----
与
Copy after login

区别:两个&时:如果有一个逻辑判断表达式为假,则后面的逻辑运算就不会执行,俗称短路现象。

一个&:两边的都会判断。不管是真是假。

逻辑或(当左右两个表达式其中有一个是true的时候,为真)

两个|符号:

$a = 2;
$b = 4;

if($a > 1 || $b < 3){ //左边的为true,右边的为false;
    echo &#39;或&#39;;
}
-----结果-----
或
Copy after login

一个|符号:

$a = 2;
$b = 4;

if($a > 1 | $b < 3){ //左边的为true,右边的为false;
    echo &#39;或&#39;;
}
-----结果-----
或
Copy after login

区别:两个|时:如果有一个逻辑判断表达式为真,则后面的逻辑运算就不会执行,短路现象。

一个|:两边的都会判断。不管是真是假。

(!true = false; !false = true)

$a = true;
var_dump(!$a);
-----结果-----
bool(false)
Copy after login

三元运算符

语法为:条件表达式?表达式1:表达式2。
说明:问号前面的位置是判断的条件,判断结果为bool型,为true时调用表达式1,为false时调用表达式2。
其逻辑为:“如果为真执行第一个,否则执行第二个。”

$a = 456;
$b = $a > 10 ? true : false; //问号前面的结果为真,执行第一个true;
var_dump($b);
-----结果-----
bool(true);
Copy after login

三元运算符可以说是if…else的简化版,在以后的开发中也会经常使用到的。

错误控制运算符

介绍: PHP 支持一个错误控制运算符:@。当将其放置在一个 PHP 表达式之前,该表达式可能产生的任何错误信息都被忽略掉

当你不想用户看到错误信息可以使用。

总结

说了这么多总算把运算符的大部分都说完了,其中还有数组运算符,类型运算符等,在数组和对象的时候会说。

 以上就是PHP基础教程三之运算符的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template