ext_121418 (
skywayman.livejournal.com
) wrote
in
funeralcrasher
2008-06-23 04:57 pm (UTC)
no subject
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
(
10 comments
)
Post a comment in response:
From:
Anonymous
This account has disabled anonymous posting.
OpenID
Identity URL:
Log in?
Dreamwidth account
Account name
Password
Log in?
If you don't have an account you can
create one now
.
Subject
HTML doesn't work in the subject.
Formatting type
Casual HTML
Markdown
Raw HTML
Rich Text Editor
Message
[
Home
|
Post Entry
|
Log in
|
Search
|
Browse Options
|
Site Map
]
no subject
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