$count = 5;
function get_count()
{
static $count = 0;
return $count++;
}
echo $count;
++$count;
echo get_count();
echo get_count();
Ben 5 0 1 çıktılar ve doğru tahmin, ama daha iyi bir açıklama gerekiyor?
Fonksiyonunda değişken $count
küresel $count
değişkeni için her türlü ilgili değildir. static
, anahtar kelime is the same as in C or Java, it means: Initialize this variable only once and keep its state when the function ends. This means, when execution re-enters the function, it sees that the inner $count has already been initialized and stored the last time as 1
, ve bu değeri kullanır.
$count = 5; // "outer" count = 5
function get_count()
{
static $count = 0; // "inner" count = 0 only the first run
return $count++; // "inner" count + 1
}
echo $count; // "outer" count is still 5
++$count; // "outer" count is now 6 (but you never echoed it)
echo get_count(); // "inner" count is now + 1 = 1 (0 before the echo)
echo get_count(); // "inner" count is now + 1 = 2 (1 before the echo)
echo get_count(); // "inner" count is now + 1 = 3 (2 before the echo)
Ben bu zihninizi temizler umuyoruz.
You have two separate variables that are both called $count, but they have a different scope. The first variable is not explicitly declared, but comes into existence when you first assign it.
(Yöntem içinde), ikinci değişken bu yöntemle yalnızca görülebilir. O static olduğundan, değeri aynı yöntemle birden çalıştırma arasında korunur. Atama $count = 0;
sadece metot çalıştırılır ilk kez yürütülür.
As for the increment operator (++), the result of the evaluation is the value before it was incremented, because the (unary) operator comes after the variable name. So yes, the output would be 5, 0, 1.
If you were to write return ++$count;
, the result would have been 5, 1, 2.
Not: ++$count
Eğer değerlendirme sonucu atılır çünkü, $count++
etkili eşdeğerdir, varolan kodu var. $count
değişkene etkisi aynıdır: o 1 artırılır alır.
İlk Yankı: size ilk satırında beyan değişken $ sayısını verir.
İkincisi: echo calles get_Count statik değişken $ sayılmasını oluşturur (yani bu fonksiyon için bağlamda bulunuyor) ve hangi sen sıfıra ayarlanarak konum statik değişkeni örneğini zaman. return $ sayaç + + biz genellikle kod önlemek bu satırlardan biri - ama değeri döndürülür SONRA aslında, artırılır.
Üçüncüsü: echo önceki arama get_Count sonrası Aynı şekilde, 0, 1 artırılır edildi, burada aynı olur - bu 1 döner ve 2 değerini artırır.
Bu yardım yapar ya da aslında daha kafa karıştırıcı olduğunu?
Eh, her şeyden önce, $ işlev içinde saymak ve $ 2 farklı değişkenler dışında fonksiyonu saymak. Neden ilk çıkış yazdırır 5 açıklıyor.
Şimdi statik için: Statik işlevi ilk kez çalıştırıldığında yerel değişkeni, yalnızca bir kez oluşturulur demektir. Her fonksiyonu yürütme sonradan aynı değişkeni kullanır, böylece son işlevi yürütme değişkenin son değeri hala var.
Ne zaman ilk çağrı get_Count () Yani, değişken 0 olarak ayarlayın ve sonra iade edilir. Döndükten sonra, değişken artırılır.
Eğer ikinci kez işlevini çağırdığınızda, değişken hala 1'dir. Bu değer döndü ve daha sonra 2 artırılır.