Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 Internet speed test! ×

Comment Re:Take PHP outside web pages altogether. (Score 2, Informative) 292

$a = array (1, 2, 3, 4);
foreach ($a as &$b) $b++;
foreach ($a as $b) $b++;
print_r ($a);
It is increment twice because after the first loop, $b is still a pointer to the fourth element of $a. Continuing in the code, the second loop will assign the fourth value of $a each value of $a, then increment it. Try debugging it like so:

$a = array (1, 2, 3, 4);
foreach($a as &$b){
$b++;
}
print_r($a);
foreach($a as $b){
print_r($a);
$b++;
print_r($a);
}
print_r($a);
... and you will see what it is doing. Solution: unset your variables before you re-use them.

$a = array (1, 2, 3, 4);
foreach ($a as &$b) $b++;
unset ($b)
foreach ($a as $b) $b++;
print_r ($a);
And for the $$ thing, it seems to do exactly what it is intended for...

[Simple]
$a = "Hello World";
$b = "a";
echo $$b;
//outputs 'Hello World'

[Complex]
$var0 = "Hell";
$var1 = "o wo";
$var2 = "rld!";
$i = "var0";
while(intval($i[3]) <= 2){
echo $$i;
$i[3] = intval($i[3])+1;
}
//outputs 'Hello World!'

Slashdot Top Deals

The price one pays for pursuing any profession, or calling, is an intimate knowledge of its ugly side. -- James Baldwin

Working...