PHP Basics Operators
1 Definition
An operator is something that can be given one or more values (in programming jargon, an expression) to produce another value (thus the entire structure becomes an expression).
Operators can be grouped according to how many values they can accept:
① Unary operators can only accept one value, such as ! (logical negation operator) or ++ (increment operator).
② Binary operators accept two values, such as the familiar arithmetic operators + (addition) and - (subtraction), which are the majority of PHP operators.
③ Ternary operator ? :, accepts three values; usually simply called the "ternary operator" (although it may be more appropriate to call it a conditional operator).
2 Operator priority
① Operator priority specifies how "closely" two expressions are bound. For example, the expression 1 + 5 * 3 evaluates to 16 instead of 18 because the multiplication sign ("*") has higher precedence than the plus sign ("+").
② If necessary, parentheses can be used to force the priority to change. For example: (1 + 5) * 3 has a value of 18.
③ If the operators have the same precedence, the combination direction of the operators determines how to operate. For example, "-" is a left-joint, then 1 - 2 - 3 is equivalent to (1 - 2) - 3 and the result is -4.
④ "=" is a right-joint, so $a = $b = $c is equivalent to $a = ($b = $c).
⑤ Operators with the same priority that are not combined cannot be used together. For example, 1 < 2 > 1 is illegal in PHP. But on the other hand the expression 1 <= 1 == 1 is legal because == has a lower precedence than <=.
⑥ The use of brackets, even when it is not necessary, clearly indicates the order of operations through the pairing of brackets, rather than relying on operator priority and associativity, which can usually increase the readability of the code.
3 Arithmetic operators
① Negation For example: -$a represents the negative value of $a.
② Addition like: $a + $b
③ Subtraction like: $a - $b
④ Multiplication like: $a * $b
⑤ Division like: $a / $b
⑥ Modulo Such as: $a % $b
⑦ Exponentiation such as: $a ** $b
Note:
a. The division operator always returns a floating point number. The only exception is that both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.
b. The operands of the modulo operator will be converted to integers (except for the decimal part) before operation.
c. The result of the modulo operator % is the same as the sign (sign) of the dividend. That is, the result of $a % $b has the same sign as $a
4 Assignment operator
① The basic assignment operator is "=". At first you may think it is "equal to", but it is not. It actually means assigning the value of the expression on the right to the operand on the left.
The value of the assignment expression is the assigned value. That is, the value of "$a = 3" is 3. In this way, you can do some tricks:
<?php $a = ($b = 4) + 5; // $a 现在成了 9,而 $b 成了 4。 ?>
② Binary arithmetic: the "combination operator" of array collection and string operators, so that its value can be used in an expression and the expression The result is assigned to it
<?php $a = 3; $a += 5; // sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello "; $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!"; ?>
③ Reference assignment: PHP supports reference assignment, using the "$var = &$othervar;" syntax. Assignment by reference means that both variables point to the same data, nothing is copied.
<?php $a = 3; $b = &$a; // $b 是 $a 的引用 print "$a\n"; // 输出 3 print "$b\n"; // 输出 3 $a = 4; // 修改 $a print "$a\n"; // 输出 4 print "$b\n"; // 也输出 4,因为 $b 是 $a 的引用,因此也被改变 ?>
④ Common sense
The assignment operation copies the value of the original variable to the new variable (pass-by-value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in dense loops.
5 位运算符
① And(按位与) $a & $b
② Or(按位或) $a | $b
③ Xor(按位异或) $a ^ $b
④ Not(按位取反) ~ $a
⑤ Shift left(左移) $a << $b
⑥ $a >> $b
6 比较运算符
① 等于 $a == $b
② 全等 $a === $b
③ 不等 $a != $b
④ 不等 $a <> $b
⑤ 不全等 $a !== $b
⑥ 小于 $a < $b
⑦ 大于 $a > $b
⑧ 小于等于 $a <= $b
⑨ 大于等于 $a >= $b
⑩ 结合比较运算符 $a <=> $b
7 错误控制运算符
PHP 支持一个错误控制运算符:@。当将其放置在一个 PHP 表达式之前,该表达式可能产生的任何错误信息都被忽略掉。
如果用 set_error_handler() 设定了自定义的错误处理函数,仍然会被调用,但是此错误处理函数可以(并且也应该)调用 error_reporting(),而该函数在出错语句前有 @ 时将返回 0。
如果激活了 track_errors 特性,表达式所产生的任何错误信息都被存放在变量 $php_errormsg 中。此变量在每次出错时都会被覆盖,所以如果想用它的话就要尽早检查。
8 执行运算符
PHP 支持一个执行运算符:反引号(``)。注意这不是单引号!PHP 将尝试将反引号中的内容作为 shell 命令来执行,并将其输出信息返回(即,可以赋给一个变量而不是简单地丢弃到标准输出)。使用反引号运算符“`”的效果与函数 shell_exec() 相同。
<?php $output = `ls -al`; echo "<pre class="brush:php;toolbar:false">$output"; ?>
注:反引号运算符在激活了安全模式或者关闭了 shell_exec() 时是无效的。
9 递增/递减运算符: PHP 支持 C 风格的前/后递增与递减运算符。
① 前加 ++$a
② 后加 $a++
③ 前减 --$a
④ 后减 $a--
10 逻辑运算符
① And(逻辑与) $a and $b
② Or(逻辑或) $a or $b
③ Xor(逻辑异或) $a xor $b
④ Not(逻辑非) ! $a
⑤ And(逻辑与) $a && $b
⑥ Or(逻辑或) $a || $b
11 字符串运算符
有两个字符串(string)运算符。第一个是连接运算符(“.”),它返回其左右参数连接后的字符串。第二个是连接赋值运算符(“.=”),它将右边参数附加到左边的参数之后。更多信息见赋值运算符。
<?php $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello "; $a .= "World!"; // now $a contains "Hello World!" ?>
12 数组运算符
① 联合 $a 和 $b 的联合。 $a + $b
② 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE。 $a == $b
③ 全等 如果 $a 和 $b 具有相同的键/值对并且顺序和类型都相同则为 TRUE。 $a === $b
④ 不等 如果 $a 不等于 $b 则为 TRUE。 $a != $b
⑤ 不等 如果 $a 不等于 $b 则为 TRUE。 $a <> $b
⑥ 不全等 如果 $a 不全等于 $b 则为 TRUE。 $a !== $b
注:+ 运算符把右边的数组元素附加到左边的数组后面,两个数组中都有的键名,则只用左边数组中的,右边的被忽略。
13 类型运算符
instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例:
<?php class MyClass { } class NotMyClass { } $a = new MyClass; var_dump($a instanceof MyClass); var_dump($a instanceof NotMyClass); ?>
以上就是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

AI Hentai Generator
Generate AI Hentai for free.

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



As a front-end developer, understanding PHP is very necessary. Although PHP is a back-end development language, mastering a certain amount of PHP knowledge can help front-end developers better understand the entire web development process, improve work efficiency, and collaborate better with back-end developers. In this article, we will discuss some PHP-related knowledge that front-end developers need to know and provide specific code examples. What is PHP? PHP (HypertextPreprocessor) is a server

PHP is a popular front-end programming language. It is powerful, easy to learn and use, and is widely used in website development and maintenance. For beginners, getting started with PHP requires a certain amount of learning and mastering. Here are some guides for beginners in PHP. 1. Learn basic concepts Before learning PHP, you need to understand some basic concepts. PHP is a scripting language that issues instructions to web servers. Simply put, you can use PHP to generate HTML code and send it to the browser to eventually render on the web page

PHP, as a scripting language widely used in web development, has become one of the necessary skills for many Internet companies to recruit technical talents. However, for some learners who are just getting started or have a weak foundation, learning PHP may encounter some difficulties. How can you better improve your basic skills when facing the PHP written test? Next, we will introduce some learning methods, hoping to help everyone improve the basic knowledge and skills of PHP. 1. Develop the habit of reading PHP official documentation. PHP official documentation is a learning

In the process of learning PHP, beginners often encounter various errors. Although this is a natural process of learning, many beginners often lose patience because of improper handling of mistakes. This article will introduce basic PHP errors and solutions, aiming to help beginners get started with PHP more easily. 1. Syntax error 1.1 Missing semicolon In PHP, statements must end with a semicolon. If you accidentally omit a semicolon, an error will be reported. For example, the following code results in an error: <?phpecho"He

Overview of PHP values: To understand the important concepts of PHP, specific code examples are required. PHP (Hypertext Preprocessor) is a scripting language widely used in web development. It can be embedded in HTML or executed as a standalone script. In web development, it is important to understand some important concepts of PHP to write efficient and maintainable code. In this article, we will introduce some important concepts of PHP and provide specific code examples to help readers understand better. variable changes

Array function is one of the most commonly used functions in PHP, which can be used to create, operate and manage arrays. When developing applications, using array functions can greatly improve development efficiency. This article will introduce some basic usage and examples of array functions in PHP to help everyone better understand and master array functions. 1. Creation and initialization of arrays Arrays in PHP can be created in the following ways: //Create arrays through the array() function $arr=array("a",&q

PHP is a very popular open source server-side scripting language that is widely used in web development. To become a good PHP programmer, reading official documentation is essential. Whether you are a beginner or an experienced developer, these tips will help you read PHP documentation more effectively. Understand the document structure PHP official documentation is divided into multiple parts, including manuals, reference manuals, FAQs, extension library documents, etc. Before you start reading, understand the structure of the document and find the parts you need. Using the search function PHP documentation

In today's Internet era of information explosion, websites have become an important way for display and promotion in all walks of life, and PHP, as the most popular server-side scripting language, is undoubtedly one of the essential skills for many website developers. To become proficient in PHP, you first need to master several key knowledge points and deepen your understanding through specific code examples. 1. PHP basic syntax The basic syntax of PHP is similar to most programming languages, including variables, data types, operators, conditional statements, loop statements, etc. Here is a simple example
