PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Sat Oct 14, 2006 12:02 pm
Could somebody point out why this code returns the following errors:
Missing argument 1 for One()
Missing argument 1 for Two()
Undefined variable: var
Code: Select all
function One($var) {
$var = 10;
return $var;
}
function Two($var) {
echo $var;
return $var;
}
Two();
Stephen,
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Oct 14, 2006 12:08 pm
The variables in the parentheses following the function name are arguments/parameters for the function. You've defined two functions that require an argument when called.
Your code doesn't even call One().
Cameri
Forum Commoner
Posts: 87 Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic
Post
by Cameri » Sat Oct 14, 2006 12:20 pm
If what you are trying to do is give the variables default values, you should do it like this:
Code: Select all
function One($var=10) {
return $var;
}
function Two($var="") {
echo $var;
return $var;
}
One(); // 10
Two(); // useless
One(20); // 20
Two("Test"); // Test
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Sat Oct 14, 2006 12:41 pm
I'm trying to access a variable from function A inside of function B.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Oct 14, 2006 1:02 pm
Code: Select all
function One($var=10) {
return $var;
}
function Two($var) {
echo $var;
return $var;
}
$one = One();
$two = Two($one);
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sat Oct 14, 2006 1:59 pm
Jcart wrote: Code: Select all
function One($var=10) {
return $var;
}
function Two($var) {
echo $var;
return $var;
}
$one = One();
$two = Two($one);
Test it be using: