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

search for in the

비트 연산자> <산술 연산자
Last updated: Sun, 25 Nov 2007

view this page in

지정 연산자

기본적인 지정 연산자는 "="이다. 이 연산자를 "같다"라고 생각할수 있느나, 그렇지 않다. 이 연산자는 좌측 피연산자에 우측의 표현식 값을 지정한다. (즉, "지정한다").

지정 표현식의 값이 지정된다. 즉, "$a = 3"의 값은 3이다. 이런 규칙으로 인해 몇가지의 교묘한(tricky) 일을 할수 있다.

$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.

기본적인 지정 연산자 외에 이진 산술과 문자열 연산자와 같은 "복합 연산자" 가 지원된다. 이런 연산자는 표현식의 값을 사용하여 그 표현식의 결과를 그 값으로 사용할수 있다. 예를 들면:

$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

지정은 원래 변수에 새로운 변수(값으로 지정된)를 복사한다. 그래서 어떤 변수를 변경하는것은 다른 변수에 아무 영향을 주지 못한다. 견고한 루프안에 큰 배열같은 것을 복사한다면 이 주제와 밀접한 관련이 있다. PHP 4는 $var = &$othervar;같은 문법으로 참조에 의한 지정을 지원한다. 그러나 PHP 3에서는 지원되지 않는다. '참조에 의한 지정'은 두 변수가 같은 데이터를 가리키도록 하고 아무것도 복사하지 않는다는 것을 의미한다. 참조에 관한 자세한 정보는 참조 표현을 참고.



비트 연산자> <산술 연산자
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
지정 연산자
Paul Ebermann
29-Apr-2008 06:07
There is also a array-append-Operator:

<?php
   $array
[]= $element;
?>

This appends the element to the end of the array, as
<?php
   array_push
($array, $element);
?>
would do.
(This is documented on the array_push page, but not here in the operator section.)
Hayley Watson
05-Feb-2008 05: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
07-Oct-2007 03: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 10:38
or you could use the xor-assignment operator..
$a ^= $b;
$b ^= $a;
$a ^= $b;
bradlis7 at bradlis7 dot com
15-Aug-2005 08: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
20-Feb-2004 10: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

비트 연산자> <산술 연산자
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites