return function variables

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
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

return function variables

Post by twb »

Hi
Does a return variable from a function have to be in the same table to have access to it. When I display a variable returned from a function I can display the result with success, however when I do the same in a different table on the same page it gives me a warning "Notice: Undefined variable".
Thanks in advance
Twb

Code: Select all

<?php 
//works fine in a table...prints ou result
$records = querry_textboxs($val_from,$val_to);
echo "test ",$records;?>

<?php 
//works fine in the same table as above
echo "test2 ",$records; ?>

<?php 
//gives me the error Undefined variable
echo "test 3", $records; ?>
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

what do you mean table? html table? no.


are you defining that variable inside a conditional statement?

like

Code: Select all

if ($foo == $bar) &#123;
    $records = querry_textboxs($val_from,$val_to);
    echo $records; // always works, $records is defined
&#125;


echo $records; // only works if $foo == $bar
Last edited by rehfeld on Thu Jan 13, 2005 3:43 pm, edited 1 time in total.
Seattlebadboy
Forum Newbie
Posts: 11
Joined: Mon Jun 16, 2003 3:55 am
Location: Seattle, WA
Contact:

Post by Seattlebadboy »

To answer your question, yes.

The reason this is, is because the return part of a function is only being set from that initial call.

The way you can get around this, is to have an intermediate variable that you set the value to equal $results, such as a $_SESSION variable, and that way you will have global access.

So, you could do something like:

Code: Select all

//grab results
$records = querry_textboxs($val_from,$val_to);

// set to session
$_SESSION&#1111;'records_set'] = $records;
Also, another way would be to embed the $_SESSION variable in the function itself.

Hope this helps.
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

return function variables

Post by twb »

Thanks Seattlebadboy
It worked great
Thanks troy
Post Reply