php .=是赋值运算符,PHP 赋值运算符

本文探讨了使用PHP进行编程时的一些技巧与注意事项,包括如何优化循环内的字符串拼接操作以提高性能,以及在处理数组时如何避免因类型检查不当而导致的问题。

用户评论:

haubertj at alfredstate dot edu (2011-09-01 07:19:40)

[[ Editor's note: You are much better off using the foreach (array_expression as $key => $value) control structure in this case ]]

When using

while ($var = current($array) {

#do stuff

next($aray)

?>

to process an array, if current($array) happens to be falsy but not === false it will still end the loop. In such a case strict typing must be used.

Like this:

while (($var = current($array)) !== FALSE) {

#do stuff

next($aray)

?>

Of course if your array may contain actual FALSE values you will have to deal with those some other way.

Peter, Moscow (2011-02-11 01:44:54)

Using $text .= "additional text"; instead of $text = $text ."additional text"; can seriously enhance performance due to memory allocation efficiency.

I reduced execution time from 5 sec to .5 sec (10 times) by simply switching to the first pattern for a loop with 900 iterations over a string $text that reaches 800K by the end.

Hayley Watson (2008-02-05 17:54:11)

You could also take adam at gmail dot com's xor-assignment operator and use the fact that it's right-associative:

$a ^= $b ^= $a ^= $b;

Hayley Watson (2007-10-07 15:22:17)

bradlis7 at bradlis7 dot com's description is a bit confusing. Here it is rephrased.

$a='a';$b='b';$a.=$b.="foo";

echo$a,"\n",$b;?>outputs

abfoo

bfoo

Because the assignment operators are right-associative and evaluate to the result of the assignment

$a.=$b.="foo";?>is equivalent to

$a.= ($b.="foo");?>and therefore

$b.="foo";$a.=$b;?>

adam at gmail dot com (2006-08-25 10:38:21)

or you could use the xor-assignment operator..

$a ^= $b;

$b ^= $a;

$a ^= $b;

bradlis7 at bradlis7 dot com (2005-08-15 08:13:01)

Note whenever you do this

$a.=$b.="bla bla";?>

it comes out to be the same as the following:

$a.=$b."bla bla";$b.="bla bla";?>

So $a actually becomes $a and the final $b string. I'm sure it's the same with numerical assignments (+=, *=...).

straz at mac dot nospam dot com (2004-02-20 22:18:19)

This page really ought to have table of assignment operators,

namely,

See the Arithmetic Operators page (http://www.php.net/manual/en/language.operators.arithmetic.php)

Assignment Same as:

$a += $b $a = $a + $b Addition

$a -= $b $a = $a - $b Subtraction

$a *= $b $a = $a * $b Multiplication

$a /= $b $a = $a / $b Division

$a %= $b $a = $a % $b Modulus

See the String Operators page(http://www.php.net/manual/en/language.operators.string.php)

$a .= $b $a = $a . $b Concatenate

See the Bitwise Operators page (http://www.php.net/manual/en/language.operators.bitwise.php)

$a &= $b $a = $a & $b Bitwise And

$a |= $b $a = $a | $b Bitwise Or

$a ^= $b $a = $a ^ $b Bitwise Xor

$a <<= $b $a = $a << $b Left shift

$a >>= $b $a = $a >> $b Right shift

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值