php 将数字添加到自身的更好方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6495848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:33:36  来源:igfitidea点击:

Better way to add number to itself?

phpvariables

提问by Lee

Is there a better/shorter way in PHP of doing

PHP 中是否有更好/更短的方法

$x = $x + 10;

i.e.

IE

something like

就像是

$x .= 10; // (but this doesn't add together)

I am sure i've seen a shorter way than doing $x = $x + 10;

我确定我见过比做更短的方法 $x = $x + 10;

回答by Yoshi

Have a look at: http://php.net/manual/language.operators.assignment.php(in the code examples and comments)

看看:http: //php.net/manual/language.operators.assignment.php(在代码示例和注释中)

You can use:

您可以使用:

$x += 10;

for example.

例如。

回答by evilone

Not a PHP guy, but $x += 10;maybe?

不是一个 PHP 人,但$x += 10;也许?

回答by PeeHaa

$x += 10;

$x += 10;

However some people find this harder to read.

然而,有些人发现这更难阅读。

What you tried ($x.= 10) works only for strings.

您尝试的 ( $x.= 10) 仅适用于字符串。

E.g.

例如

$x = 'test';
$x.= 'ing...';

回答by Felix Kling

Like in many other languages:

就像在许多其他语言中一样:

$x += 10;

More information: Assignment Operators

更多信息:赋值运算符

回答by ae0709

$x+=10;

Is this what you're wanting?

这是你想要的吗?

回答by rockerest

$x += 10;adds 10 to $x

$x += 10;加 10 $x

or

或者

$x += $x;adds $xto itself, but you could just do: $x *= 2;

$x += $x;添加$x到自身,但你可以这样做:$x *= 2;

回答by Senthess

You are looking for this:

你正在寻找这个:

$x += 10;

回答by burchyd

$x +=10; is equivalent to $x = $x +10;

$x +=10; 相当于 $x = $x +10;

http://www.tizag.com/phpT/operators.php

http://www.tizag.com/phpT/operators.php

回答by Gideon Babu

it's pretty simple. $x += 10; is same as $x = $x + 10;

这很简单。$x += 10; 与 $x = $x + 10 相同;