ext_121418 ([identity profile] skywayman.livejournal.com) wrote in [personal profile] funeralcrasher 2008-06-23 04:57 pm (UTC)

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

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting