Table of Contents
Content explained in this section
Increment and decrement operators
Assignment operator
string Operator
Comparison operators
Bit operators
A brief introduction to binary
逻辑运算符
三元运算符
错误控制运算符
总结
Home Backend Development PHP Tutorial PHP basic tutorial three operators

PHP basic tutorial three operators

Mar 01, 2017 am 09:28 AM

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中文网(www.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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles