Return

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

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Return

Post by impulse() »

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,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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().
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

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() »

I'm trying to access a variable from function A inside of function B.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

function One($var=10) {
  return $var;
}
 
function Two($var) {
  echo $var;
  return $var;
}

$one = One(); 
$two = Two($one);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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:

Code: Select all

<?php
echo $two;
?>
Post Reply