search
Homephp教程php手册PHP中的运算符(1)

PHP中的运算符(1)

Jun 13, 2016 am 11:01 AM
phpNopriorityofbindingexpressionoperator

运算符优先级

运算符优先级指定了两个表达式绑定得有多“紧密”。例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是因为乘号(“*”)的优先级比加号(“+”)高。必要时可以用括号来强制改变优先级。例如:(1 + 5) * 3 的值为 18。

下表从低到高列出了运算符的优先级。

运算符优先级

结合方向 运算符
,
or
xor
and
print
= += -= *= /= .= %= &= |= ^= ~= >=
? :
||
&&
|
^
&
== != === !==
>=
>
+ - .
* / %
! ~ ++ -- (int) (float) (string) (array) (object) @
[
new

注: 尽管 ! 比 = 的优先级高,PHP 仍旧允许类似如下的表达式:if (!$a = foo()),在此例中 foo() 的输出被赋给了 $a。

算术运算符

还记得学校里学到的基本数学知识吗?就和它们一样。

算术运算符

例子 名称 结果
$a + $b 加法 $a 和 $b 的和。
$a - $b 减法 $a 和 $b 的差。
$a * $b 乘法 $a 和 $b 的积。
$a / $b 除法 $a 除以 $b 的商。
$a % $b 取模 $a 除以 $b 的余数。

除号(“/”)总是返回浮点数,即使两个运算数是整数(或由字符串转换成的整数)也是这样。

赋值运算符

基本的赋值运算符是“=”。你一开始可能会以为它是“等于”,其实不是的。它实际上意味着把右边表达式的值赋给左运算数。

赋值运算表达式的值也就是所赋的值。也就是说,“$a = 3”的值是 3。这样就可以使你做一些小技巧:

$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4. 

在基本赋值运算符之外,还有适合于所有二元算术和字符串运算符的“组和运算符”,这可以让你在一个表达式中使用它的值并把表达式的结果赋给它,例如:

$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!";

注意赋值运算将原变量的值拷贝到新变量中(传值赋值),所以改变其中一个并不影响另一个。这也适合于你在在紧密循环中拷贝一些值例如大数值。PHP 4 支持引用赋值,用 $var = &$othervar; 语法,但在 PHP 3 中不可能这样做。“引用赋值”意味着两个变量都指向同一个数据,没有任何数据的拷贝。

1

Statement
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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

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.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.