SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    maine
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    returning values to includes

    How do you return values from a function to a separate include file?
    I have tried something like this in my extras/functions.php file:

    function test ($x)
    {
    if ($x > 0)
    $answer = "true";
    else
    $answer = "false";
    return $answer;
    }

    then I have a file called includes/substance.inc that calls it:

    require ("extras/functions.php");
    $x=3;
    test($x);
    echo $answer;

    but nothing is output. I get an error that it is an "Undefined Index."
    answers?

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're not assigning the function's value to anything. For example, try this:
    PHP Code:
    $answer test($x); 

  3. #3
    SitePoint Zealot DarkWulf's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    require ("extras/functions.php");
    $x=3;
    test($x);
    echo 
    $answer
    You want either:
    PHP Code:
    echo test($x); 
    or:
    PHP Code:
    $answer test($x);
    echo 
    $answer
    When using return, you are having that function "stand for" that variable in a sense, so you need to capture the value of $answer, or do something with it immediately.

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imagine8
    How do you return values from a function to a separate include file?
    I have tried something like this in my extras/functions.php file:

    function test ($x)
    {
    if ($x > 0)
    $answer = "true";
    else
    $answer = "false";
    return $answer;
    }

    then I have a file called includes/substance.inc that calls it:

    require ("extras/functions.php");
    $x=3;
    test($x);
    echo $answer;

    but nothing is output. I get an error that it is an "Undefined Index."
    answers?
    You must assign the returned value to a variable or simply echo it out, like so:
    PHP Code:
    $answer test($x); 
    or
    PHP Code:
    echo test($x); 
    HTH
    John

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha! My trigger finger is still the fastest.

  6. #6
    SitePoint Zealot DarkWulf's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I was close enough behind you. And I had even more quality PHP-goodness. So you shot and clipped the target, and I shot and nailed the bullseye.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    maine
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Congratulations, you're both fast with your fists. I'm sure the ladies are pleased.

    your advice helped, but I'm working with a while loop and its only printing the last entry:

    while ($row = $db->fetchAssoc())
    {
    extract($row);

    $description = stripslashes($description);
    $href = stripslashes($href);
    $title = stripslashes($title);
    $et_type = stripslashes($et_type);

    if ($et_type=='1') //Get HTML/text directly from database
    {
    $result .= "<li><p><a href='".$href." target='_blank'>".$title."</a>\n<br />\n".$description."</p></li>\n";
    }
    else //Text is a reference to a file
    {
    include("$title");
    }
    return $result;
    }

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imagine8
    your advice helped, but I'm working with a while loop and its only printing the last entry:
    And why wouldn't it? return is supposed to end the execution of things. Read up at that link I just posted.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    maine
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Ah, I got it working. I just had the

    return $result;

    inside the wrong brackets. Once I moved it outside of the while loop, it works fine.
    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •