downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

비트 연산자> <계산 연산자
[edit] Last updated: Sat, 07 Jan 2012

view this page in

할당 연산자

기본 할당 연산자는 "="입니다. 처음에는 이것을 "같다"로 생각할 수 있습니다. 아닙니다. 실제로는 왼쪽 연산수가 오른쪽 표현의 값으로 설정됨을 의미합니다. ("를 설정"입니다)

할당 연산자의 값은 할당된 값입니다. 그러므로, "$a = 3"의 값은 3입니다. 이는 트릭 같은 일을 허용합니다:

<?php

$a 
= ($b 4) + 5// $a는 이제 9와 같고, $b는 4로 설정됩니다.

?>

기본 연산자에 추가로, 모든 이항 계산, 배열 합집합과 문자열 연산자에 대하여 "결합 연산자"가 존재합니다. 이를 사용해서 값을 표현으로 사용하고 그 표현의 결과를 값에 설정할 수 있도록 합니다. 예를 들면:

<?php

$a 
3;
$a += 5// $a를 8로 설정, 다음과 같습니다: $a = $a + 5;
$b "Hello ";
$b .= "There!"// $b = $b . "There!";와 마찬가지로, $b를 "Hello There!"로 설정

?>

할당은 원래 값을 새로운 것으로 복사하기 때문에(값으로 할당), 하나의 변경은 다른 것에 영향을 주지 않습니다. 또한 빠듯한 루프 안에서 커다란 배열을 복사해야할 필요성이 존재하게 됩니다. $var = &$othervar; 구문을 사용해서, 참조로 할당도 지원합니다. '참조로 할당'은 두 변수가 같은 데이터를 가리키는 것을 의미하며, 아무것도 복사하지 않습니다. 참조에 대해서 알아보려면, 참조 설명을 읽어보십시오. PHP 5부터, 객체는 명시적으로 새로운 것을 만드는 clone 키워드를 사용하지 않는 한 참조로 할당됩니다.



비트 연산자> <계산 연산자
[edit] Last updated: Sat, 07 Jan 2012
 
add a note add a note User Contributed Notes 할당 연산자
haubertj at alfredstate dot edu 01-Sep-2011 04:19
[[   Editor's note: You are much better off using the foreach (array_expression as $key => $value) control structure in this case   ]]

When using

<php
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:

<php
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 11-Feb-2011 10:44
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 06-Feb-2008 02:54
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 08-Oct-2007 12:22
bradlis7 at bradlis7 dot com's description is a bit confusing. Here it is rephrased.

<?php
$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
<?php
$a
.= $b .= "foo";
?>
is equivalent to
<?php
$a
.= ($b .= "foo");
?>
and therefore
<?php
$b
.= "foo";
$a .= $b;
?>
adam at gmail dot com 25-Aug-2006 07:38
or you could use the xor-assignment operator..
$a ^= $b;
$b ^= $a;
$a ^= $b;
bradlis7 at bradlis7 dot com 15-Aug-2005 05:13
Note whenever you do this

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

it comes out to be the same as the following:

<?php
$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 21-Feb-2004 07:18
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

 
show source | credits | stats | sitemap | contact | advertising | mirror sites