Atama İşleçleri

Temel atama işleci "=" imidir. Programlamaya yeni başlayanlar bu işleci"eşittir" diye okurlar. Aslında yapılan işlem bir şeyleri birbirlerine eşitlemek değildir. Yaptığı iş sağındaki ifadenin değerini solundaki terimin değeri haline getirmektir, yani imleci içeren ifadeyi birşey'in değeri diye okumak daha doğrudur.

Bir atama ifadesinin değeri atanan değerdir. Yani, "$a = 3" ifadesinin değeri 3'tür. Bunun bir takım yan sonuçları da vardır:

<?php

$a
= ($b = 4) + 5; // $a'nın değeri 9, $b'nin değeri 4 olur.

?>

Temel atama işlecinden başka atama işlemini temel aritmetik, ikil aritmetik işlemleriyle veya dizi ya da dizge birleştirme işlemleriyle birleştiren, "birleşik atama işleçleri" vardır. Örnek:

<?php

$a
= 3;
$a += 5; // $a'nın değeri 8 olur, asıl işlem: $a = $a + 5;
$b = "Herkese ";
$b .= "Merhaba!"; // $b'nin değeri "Herkese Merhaba!" olur.
// asıl işlem: $b = $b . "Merhaba!";

?>

Atama işleminin özgün değişkeni yeni değişkene kopyaladığına (değeriyle atadığına) dikkat ediniz. Dolayısıyla birinde yapılan değişiklik diğerini etkilemeyecektir. Kapalı bir döngü içinde büyükçe bir diziye atama işlemleri yapma ihtiyacı duyarsanız bu ayrı bir anlam kazanır.

PHP içindeki değere göre olağan atama davranışının bir istisnası, gönderimle atanan nesnelerle gerçekleşir. Nesneler clone anahtar sözcüğü ile doğrudan kopyalanabilir.

Gönderimli atama

Gönderimli atama $değişken = &$diğerdeğişken; sözdizimi ile desteklenmektedir. 'Gönderimli atama', iki değişkenin aynı veriyi gösterdiği ve birbirlerinden birşeyler kopyalamadıkları anlamına gelir.

Örnek 1 - Gönderimli atama

<?php
$a
= 3;
$b = &$a; // $b, $a'ya bir gönderimdir

print "$a\n"; // 3 basar
print "$b\n"; // 3 basar

$a = 4; // $a değişir

print "$a\n"; // 4 basar
print "$b\n"; // bu da 4 basar, $b, $a'ya bir gönderimdi ve $a değişti

?>

new işleci otomatik olarak bir gönderim döndürür. Dolayısıyla, new sonucunu gönderimli atamak bir hatadır.

Örneğin bu kod bir hata veya uyarı ile sonuçlanır:

<?php
class C {}

$o = &new C;
?>

Yukarıdaki örneğin çıktısı:

Parse error: syntax error, unexpected 'new' (T_NEW) in …
// Çözümleme hatası: sözdizimi hatası, beklenmeyen 'new' (T_NEW) in …

Gönderimler hakkında daha ayrıntılı bilgi edinmek için Gönderimlerle ilgili herşey bölümüne bakınız.

Aritmetik Atams İşleçleri

Örnek Eşdeğeri İşlem
$a += $b $a = $a + +$b Toplama
$a -= $b $a = $a - $b Çıkarma
$a *= $b $a = $a * $b Çarpma
$a /= $b $a = $a / $b Bölme
$a %= $b $a = $a % $b Mod
$a **= $b $a = $a ** $b Üs alma

Bitsel Atama İşleçleri

Örnek Eşdeğeri İşlem
$a &= $b $a = $a & $b Bitsel VE
$a |= $b $a = $a | $b Bitsel VEYA
$a ^= $b $a = $a ^ $b Bitsel XOR
$a <<= $b $a = $a << $b Sola Öteleme
$a >>= $b $a = $a >> $b Sağa Öteleme

Diğer Atama İşleçleri

Örnek Eşdeğeri İşlem
$a .= $b $a = $a . $b Dize Birleştirme
$a ??= $b $a = $a ?? $b Null Birleşimi
add a note add a note

User Contributed Notes 8 notes

up
94
Peter, Moscow
13 years ago
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.
up
55
Robert Schneider
9 years ago
Be aware of assignments with conditionals. The assignment operator is stronger as 'and', 'or' and 'xor'.

<?php
$x
= true and false;   //$x will be true
$y = (true and false); //$y will be false
?>
up
29
Hayley Watson
16 years ago
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;
?>
up
6
asc at putc dot de
8 years ago
PHP uses a temporary variable for combined assign-operators (unlike JavaScript), therefore the left-hand-side (target) gets evaluated last.

Input:
$a += $b + $c;

Meaning:
$a = ($b + $c) + $a;

Not:
$a = $a + ($b + $c);

This can be important if the target gets modified inside the expression.

$a = 0;
$a += (++$a) + (++$a); // yields 5 (instead of 4)
up
-14
ma dot bx dot ar at gamil dot com
9 years ago
Document says:
"An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference in PHP 5. Objects may be explicitly copied via the clone keyword."

But it's not very accurate! Considering this code:
<?php
$a
= new StdClass;
$b = $a;

$a = new StdClass;

var_dump ($a, $b);
?>

Output:
object(stdClass)#2 (0) {
}
object(stdClass)#1 (0) {
}
Note: #2 and #1 means two different objects.

But this code:
<?php
$a
= new StdClass;
$b = &$a;

$a = new StdClass;

var_dump ($a, $b);
?>

Output will be:

object(stdClass)#2 (0) {
}
object(stdClass)#2 (0) {
}

Note: Still pointing to the same object.

And this shows that that exception is not valid, PHP assignment for objects still makes a copy of variable and does not creates a real reference, albeit changing an object variable members will cause both copies to change.
So, I would say assignment operator makes a copy of 'Object reference' not a real object reference.
up
-15
Hayley Watson
16 years ago
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;
up
-31
bradlis7 at bradlis7 dot com
18 years ago
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 (+=, *=...).
up
-51
haubertj at alfredstate dot edu
12 years ago
[[   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.
To Top