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

search for in the

Bitsel İşleçler> <Aritmetik İşleçler
[edit] Last updated: Fri, 23 Mar 2012

view this page in

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.

?>

Diziler içi bir değerin bir isimli anahtara atanması "=>" işleci kullanılarak gerçekleştirilir. Bu işlecin önceliği diğer atama işleçlerinin önceliğiyle aynıdır.

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. Gönderimli atama $değişken = &$diğerdeğişken; sözdizimi ile desteklenmektedir. 'Gönderimli atama' denince, iki değişkenin aynı veriyi gösterdiğini ve birbirlerinden birşeyler kopyalamadıklarını anlıyoruz. Gönderimler hakkında daha ayrıntılı bilgi edinmek için Gönderimlerle ilgili herşey bölümüne bakınız. PHP 5'ten itibaren, clone anahtar sözcüğü ile yeni bir nesneye atama yapılmadıkça, nesneler gönderimli olarak atanırlar.



Bitsel İşleçler> <Aritmetik İşleçler
[edit] Last updated: Fri, 23 Mar 2012
 
add a note add a note User Contributed Notes Atama İşleçleri
haubertj at alfredstate dot edu 01-Sep-2011 07: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 01: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 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

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