这是什么?

这是一个关于PHP语法的问题集合.这也是一个社区维基,所以每个人都被邀请参与维护这个列表.

这是为什么呢?

It used to be hard to find questions about operators and other syntax tokens.¹
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from the PHP Manual.

注:自2013年1月起,堆栈溢出does support special characters.只需将搜索词用引号括起来,例如[php] "==" vs "==="

我该怎么办?

如果你在这里被人指出来是因为你问了这样一个问题,请在下面找到特定的语法.PHP manual的链接页面以及链接的问题很可能会回答您的问题.如果是这样的话,我们鼓励你对答案投赞成票.这份 list 并不能代替其他人提供的帮助.

名单

如果您的特定令牌未在下面列出,您可能会在List of Parser Tokens中找到它.


& Bitwise OperatorsReferences


=& References


&= Bitwise Operators


&& Logical Operators


% Arithmetic Operators


!! Logical Operators


@ Error Control Operators


?: Ternary Operator


??Null Coalesce Operator(自PHP7起)


?string ?int ?array ?bool ?float Nullable return type declaration(从PHP 7.1开始)


: Alternative syntax for control structuresTernary OperatorReturn Type Declaration


:: Scope Resolution Operator


\ Namespaces


-> Classes And Objects


=> Arrays


^ Bitwise Operators


>> Bitwise Operators


<< Bitwise Operators


<<< Heredoc or Nowdoc


= Assignment Operators


== Comparison Operators


=== Comparison Operators


!== Comparison Operators


!= Comparison Operators


<> Comparison Operators


<=>Comparison Operators(自PHP7.0以来)


| Bitwise Operators


|| Logical Operators


~ Bitwise Operators


+ Arithmetic Operators, Array Operators


+=-= Assignment Operators


++-- Incrementing/Decrementing Operators


.= Assignment Operators


. String Operators


, Function Arguments

, Variable Declarations


$$ Variable Variables


` Execution Operator


<?= Short Open Tags


[] Arrays(PHP 5.4之后的简短语法)


<? Opening and Closing tags


...Argument unpacking(自PHP5.6起)


**Exponentiation(自PHP5.6起)


# One-line shell-style comment


?->NullSafe Operator Calls(自PHP 8.0以来)


推荐答案

100

++递增运算符

--减量运算符

Example    Name              Effect
---------------------------------------------------------------------
++$a       Pre-increment     Increments $a by one, then returns $a.
$a++       Post-increment    Returns $a, then increments $a by one.
--$a       Pre-decrement     Decrements $a by one, then returns $a.
$a--       Post-decrement    Returns $a, then decrements $a by one.

这些可以在变量之前或之后.

如果放在变量之前,则对变量first执行递增/递减操作,然后返回结果.如果放在变量后面,变量返回first,那么递增/递减操作就完成了.

例如:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
    echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

100

在这种情况下使用++$i以上,因为它更快.$i++也会有同样的结果.

预递增要快一点,因为它确实会递增变量,然后"返回"结果.后增量创建一个特殊变量,将第一个变量的值复制到那里,只有在使用了第一个变量之后,才用第二个变量的值替换它的值.

但是,您必须使用$apples--,因为首先,您要显示当前的苹果数,then您要从中减go 一个.

您还可以在PHP中递增字母:

$i = "a";
while ($i < "c") {
    echo $i++;
}

一旦达到z,下一个就是aa,依此类推.

请注意,字符变量可以递增,但不能递减,即使如此,也只支持纯ASCII字符(a-z和A-Z).


Stack Overflow Posts:

  • 100

Php相关问答推荐

Laravel 11未自动注册策略

如何使用Ubuntu在同一台服务器上为多个版本构建相同的php扩展?

尽管包含目标日期,但日期范围比较仍有意外输出

FatFreeFramework上的不同路由

PHP中的圆生成器在国际日期变更线上失败

Mod_重写子目录中相应的php文件

莫德克斯.当我try 使用pThumb为手机压缩图像时,没有任何react

哪里的表现能更好?

Rappasoft Datatables V2(我想在另一个表具有相同值列时显示数据)

启用 WooCommerce 我的帐户付款方式,允许客户添加付款方式

用自定义代码替换 WooCommerce 单一产品简短描述

如何从 URL 核心 PHP 中的 API 获取 JSON?

WooCommerce 按产品运输插件:仅收取较高的运输费用

从插件更改主题功能,function_exists 不起作用

WooCommerce 有关新订单的附加邮箱通知,其中包含具体详细信息

PHP:如何将多列sql查询结果转换成数组

Xdebug 不会在断点处停止

使用 Process facade 在外部 Laravel 项目上运行 Artisan 命令会返回主项目的数据库错误

构建 [Controller] 时目标 [Interface] 不可实例化

如何为多个表的id列设置不同的值