기본 대입 연산자는 "="이다. 이 연산자를 처음 보았을 때는 "같다"라는 연산자로 생각하기 쉽지만 전혀 아니다. 이것의 의미는 오른쪽의 표현식을 계산하여 그 값을 왼쪽에 있는 피연산자의 값으로 설정한다는 의미이다. (이것을 "gets set to"라고 한다.)
대입 연산자의 값은 왼쪽으로 대입된 값이다. 즉, "$a = 3"의 값은 3이 된다. 이것은 여러분에게 약간의 트릭이 가능하도록한다.
기본적인 대입 연산자 외에 모든 Bit 단위 연산자, 산술 연산자와 결합한 복합 대입 연산자도 있다. 결합된 연산자는 표현식에서 해당 연산으로 사용되고, 그 연산 값을 왼쪽에 있는 피연산자에 대입한다. 예를 들어 :
$a = 3; $a += 5; // $a는 8이다. $a = $a + 5; 와 동일하다. $b = "Hello "; $b .= "There!"; // $b는 "Hello There!"가 된다. $b = $b . "There!";와 같다. |
Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. PHP 4 supports assignment by reference, using the $var = &$othervar; syntax, but this is not possible in PHP 3. 'Assignment by reference' means that both variables end up pointing at the same data, and nothing is copied anywhere. To learn more about references, please read References explained.