参考——这个符号在 PHP 中是什么意思? [英] Reference — What does this symbol mean in PHP?

查看:34
本文介绍了参考——这个符号在 PHP 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么?

这是一个不时出现的关于 PHP 语法的问题集合.这也是一个社区维基,所以邀请大家参与维护这个列表.

这是为什么?

过去很难找到有关运算符和其他语法标记的问题.¹
主要思想是在 Stack Overflow 上提供指向现有问题的链接,这样我们就可以更轻松地引用它们,而不是从 PHP 手册中复制内容.

注意:自 2013 年 1 月起,Stack Overflow 确实支持特殊字符.只需用引号将搜索词括起来,例如[php] "==》对比==="

我应该在这里做什么?

如果有人指出您是因为您提出了这样的问题,请在下面找到特定的语法.PHP 手册 的链接页面以及链接的问题很可能会回答您的问题.如果是这样,我们鼓励您对答案投赞成票.此列表不能替代其他人提供的帮助.

名单

如果您的特定令牌未在下面列出,您可能会在解析器列表中找到它令牌.


& 位运算符参考资料


=& 参考资料


&= 位运算符


&& 逻辑运算符


% 算术运算符


!! 逻辑运算符


@ 错误控制运算符


?: 三元运算符


?? 空合并运算符(自 PHP 7 起)


?string?int?array?bool?float 可空返回类型声明(自 PHP 7.1 起)


: 控制的替代语法结构, 三元运算符, 返回类型声明


:: 范围解析运算符


\ 命名空间


-> 类和对象


=> 数组


^ 位运算符


>> 位运算符


<< 位运算符


<<< Heredoc 或 Nowdoc


= 赋值运算符


== 比较运算符


=== 比较运算符


!== 比较运算符


!= 比较运算符


<> 比较运算符


<=> 比较运算符(自 PHP 7.0 起)


| 位运算符


|| 逻辑运算符


~ 位运算符


+ 算术运算符, 数组运算符


+=-= 赋值运算符


++-- 递增/递减运算符


.= 赋值运算符


. 字符串运算符


, 函数参数

, 变量声明


$$ 变量变量


` 执行运算符


<?= 短打开标签


[] 数组(自 PHP 5.4 起的简短语法)


<? 开始和结束标签


... 参数解包(自 PHP 5.6 起)


** 求幂(自 PHP 5.6 起)


# 单行外壳样式评论


?-> NullSafe 操作符调用(自 PHP 8.0 起)


解决方案

递增/递减运算符

++ 增量运算符

-- 递减运算符

示例名称效果---------------------------------------------------------------------++$a Pre-increment 将 $a 增加 1,然后返回 $a.$a++ 后增量返回 $a,然后将 $a 加一.--$a Pre-decrement 将 $a 减一,然后返回 $a.$a-- 递减后返回 $a,然后将 $a 减一.

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

如果放在变量之前,则对变量进行递增/递减操作,然后返回结果.如果放在变量之后,则变量返回,然后进行递增/递减操作.

例如:

$apples = 10;for ($i = 0; $i <10; ++$i) {回声我有".$apples-- ." 苹果.我只吃了一个.\n";}

实例

在上面的例子中使用 ++$i,因为它更快.$i++ 会得到相同的结果.

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

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

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

$i = "a";而 ($i < "c") {回声 $i++;}

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

<块引用>

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

<小时>

堆栈溢出帖子:

What is this?

This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.

Why is this?

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.

Note: Since January 2013, Stack Overflow does support special characters. Just surround the search terms by quotes, e.g. [php] "==" vs "==="

What should I do here?

If you have been pointed here by someone because you have asked such a question, please find the particular syntax below. The linked pages to the PHP manual along with the linked questions will likely answer your question then. If so, you are encouraged to upvote the answer. This list is not meant as a substitute for the help others provided.

The List

If your particular token is not listed below, you might find it in the List of Parser Tokens.


& Bitwise Operators or References


=& References


&= Bitwise Operators


&& Logical Operators


% Arithmetic Operators


!! Logical Operators


@ Error Control Operators


?: Ternary Operator


?? Null Coalesce Operator (since PHP 7)


?string ?int ?array ?bool ?float Nullable return type declaration (since PHP 7.1)


: Alternative syntax for control structures, Ternary Operator, Return 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 (since PHP 7.0)


| Bitwise Operators


|| Logical Operators


~ Bitwise Operators


+ Arithmetic Operators, Array Operators


+= and -= Assignment Operators


++ and -- Incrementing/Decrementing Operators


.= Assignment Operators


. String Operators


, Function Arguments

, Variable Declarations


$$ Variable Variables


` Execution Operator


<?= Short Open Tags


[] Arrays (short syntax since PHP 5.4)


<? Opening and Closing tags


... Argument unpacking (since PHP 5.6)


** Exponentiation (since PHP 5.6)


# One-line shell-style comment


?-> NullSafe Operator Calls (since PHP 8.0)


解决方案

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

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.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

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

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

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

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

这篇关于参考——这个符号在 PHP 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆