funeralcrasher: (Default)
$num = 10;
function multiply()
{
$num = $num * 10;
}
multiply();
echo $num;


This prints "10" to the screen.

$num = $num * 10;
echo $num;


But this prints "0".

Why is that?

Date: 2008-06-23 02:50 pm (UTC)From: [identity profile] pkbarbiedoll.livejournal.com
Right, familiar with the idea of 10 * 0 = 0. :o)

Why does the function print 10?

Date: 2008-06-23 03:12 pm (UTC)From: [identity profile] outintospace.livejournal.com
Because $num is not returned by the function. When multiply() is called, it's not updating the reference to $num. It's creating a new variable with method scope called $num.

Profile

funeralcrasher: (Default)
funeralcrasher

June 2020

S M T W T F S
 123456
78910111213
141516 1718 1920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 6th, 2025 07:42 am
Powered by Dreamwidth Studios