PHP basic tutorial three operators
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
$a = 12; $b = 12; $res = $a + $b; echo $res; -----结果----- 24
$a = 12; $b = 6; $res = $a - $b; echo $res; -----结果----- 6
$a = 2; $b = 3; $res = $a * $b; echo $res; -----结果----- 6
$a = 12; $b = 6; $res = $a / $b; echo $res; -----结果----- 2
$a = 3; $b = 2; $res = $a % $b; echo $res; -----结果----- 1
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 . '<br>' . $a; -----结果----- $b = 2 $a = 2
$a++
$a = 1; $b = $a++;//先赋值,在进行++运算,等价于$b = $a; $a = $a + 1; echo $b . '<br>' . $a; -----结果----- $b = 1 $a = 2
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 = 'string';
+=, -=, /=, etc.
$a = 1; $a += 2;//等价于$a = $a + 2; echo $a; -----结果----- 3
string Operator
has two strings. The operator is ("."), which returns a string concatenated with its left and right arguments.$a = 1; $b = 2; echo '$a = ' . $a . ' $b = ' . $b; -----结果----- $a = 1 $b = 2;
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 = '2'; 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 inbinary to evaluate and operate.
Before that, let’s talk about binary firstA 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
Convert binary to decimal: "Expand sum by weight"
1111 = 1 * 20 + 1 * 21 + 1 * 22 + 1 * 23 = 15 10 = 0 * 20 + 1 * 21 = 2
The symbol bit in the computer has three expression forms:
- Original code
- Inverse code
- Complement code
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.
原码 10000000 00000000 00000000 00000001 反码 11111111 11111111 11111111 11111110 取反 补码 11111111 11111111 11111111 11111111 加1
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
或(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
异或(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
取反(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
左移(将 $a 中的位向左移动 $b 次(每一次移动都表示“乘以 2”))
左移时右侧以零填充,符号位被移走意味着正负号不被保留。
$a = 1; // 二进制: // 00000000 00000000 00000000 00000001 // 左移时右侧以零填充,符号位被移走意味着正负号不被保留。 // 00000000 00000000 00000000 00000100 == 4 echo $a << 2;//将 $a 中的位向左移动2次 -----结果----- 4
右移时左侧以符号位填充,意味着正负号被保留。
$a = 4; // 右移时左侧以符号位填充,意味着正负号被保留。 // 00000000 00000000 00000000 00000100 // 00000000 00000000 00000000 00000001 == 1 echo $a >> 2; -----结果----- 1
到这里位运算符算术基本说完了……
逻辑运算符
逻辑运算符也就是判断逻辑,它的返回结果一般是布尔类型,像与或非等;而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 '与'; } -----结果----- 与
一个&符号:
$a = 2; $b = 3; if($a > 1 & $b > 2){ //左边的为true,右边的为true;两边都为true,结果为true; echo '与'; } -----结果----- 与
区别:两个&时:如果有一个逻辑判断表达式为假,则后面的逻辑运算就不会执行,俗称短路现象。
一个&:两边的都会判断。不管是真是假。
逻辑或(当左右两个表达式其中有一个是true的时候,为真)
两个|符号:
$a = 2; $b = 4; if($a > 1 || $b < 3){ //左边的为true,右边的为false; echo '或'; } -----结果----- 或
一个|符号:
$a = 2; $b = 4; if($a > 1 | $b < 3){ //左边的为true,右边的为false; echo '或'; } -----结果----- 或
区别:两个|时:如果有一个逻辑判断表达式为真,则后面的逻辑运算就不会执行,短路现象。
一个|:两边的都会判断。不管是真是假。
非(!true = false; !false = true)
$a = true; var_dump(!$a); -----结果----- bool(false)
三元运算符
语法为:条件表达式?表达式1:表达式2。
说明:问号前面的位置是判断的条件,判断结果为bool型,为true时调用表达式1,为false时调用表达式2。
其逻辑为:“如果为真执行第一个,否则执行第二个。”
$a = 456; $b = $a > 10 ? true : false; //问号前面的结果为真,执行第一个true; var_dump($b); -----结果----- bool(true);
三元运算符可以说是if…else的简化版,在以后的开发中也会经常使用到的。
错误控制运算符
介绍: PHP 支持一个错误控制运算符:@。当将其放置在一个 PHP 表达式之前,该表达式可能产生的任何错误信息都被忽略掉
当你不想用户看到错误信息可以使用。
总结
说了这么多总算把运算符的大部分都说完了,其中还有数组运算符,类型运算符等,在数组和对象的时候会说。
以上就是PHP基础教程三之运算符的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
