funeralcrasher: (Default)
funeralcrasher ([personal profile] funeralcrasher) wrote2008-06-23 10:33 am

Stumped.

$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?

[identity profile] outintospace.livejournal.com 2008-06-23 02:46 pm (UTC)(link)
Because $num in the second example is initialized as nothing (or zero, conceptually). Zero * 10 = zero.

Try:

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

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

Why does the function print 10?

[identity profile] outintospace.livejournal.com 2008-06-23 03:12 pm (UTC)(link)
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.

[identity profile] poeticalpanther.livejournal.com 2008-06-23 02:48 pm (UTC)(link)
I'm guessing, I admit, but is it because you're calling a different variable? $a instead of $num?

[identity profile] pkbarbiedoll.livejournal.com 2008-06-23 02:53 pm (UTC)(link)
good catch.. I made a type-o when posting.

The problem remains, though - if $x * 2 = 0 (where x is not set), then why is the function returning a number instead of 0?

[identity profile] skywayman.livejournal.com 2008-06-23 04:57 pm (UTC)(link)
Variable scope. See my response.

[identity profile] skywayman.livejournal.com 2008-06-23 04:57 pm (UTC)(link)
Variable scope is your culprit.

The problem is that $num inside multiply() is not the same $num you set to 10. It is a new $num, local to that function.

If you want that function to work you either have to pass the value and return it, or reference the global variable.

// Pass example:

$num = 10;

function multiply($num)
{
$num = ($num * 10);
return $num;
}

$num = multiply($num);

echo $num;

// Global reference example:

$num = 10;

function multiply()
{
global $num;
$num = ($num * 10);
}

multiply();

echo $num;


Required reading: http://us3.php.net/variables.scope

[identity profile] pkbarbiedoll.livejournal.com 2008-06-23 07:21 pm (UTC)(link)
I realize that $num = 10; won't be carried to the function without a global definition inside the function.

What I don't understand is why $num = $num * 10; equals 10, when $num is undefined (ie, 0 right?)

[identity profile] open-other-end.livejournal.com 2008-06-24 07:11 am (UTC)(link)
$num = $num * 10 doesnt mean $num = 10. What's happening is you've set $num to 10, defined a function, ran a function that didn't actually affect $num, and then you've printed out $num again. Nothing's actually changed.

Is this Javascript? I don't know it too well, but I don't know how function declarations work. You need to say that $num is a parameter inside the function, and then return it back again. If you say instead something like

$num = 10
function multiply(var num)
{
$num = $num * 10;
return $num;
}
$num = multiply($num)
and then print out num, you should get 100.

[identity profile] pkbarbiedoll.livejournal.com 2008-06-24 10:59 am (UTC)(link)
Duh! I just saw that.. Because there isn't a global declaration in the function, the variable doesn't exist to the "outside world". I feel teh stoopid this am. lol.
thanks for the drawn out expl.