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 1 is illegal in PHP. But on the other hand the expression 1 ⑥ 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 ⑥ $a >> $b
6 比较运算符
① 等于 $a == $b
② 全等 $a === $b
③ 不等 $a != $b
④ 不等 $a $b
⑤ 不全等 $a !== $b
⑥ 小于 $a ⑦ 大于 $a > $b
⑧ 小于等于 $a ⑨ 大于等于 $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)!

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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