Page 1 of 1

return function variables

Posted: Thu Jan 13, 2005 3:03 pm
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; ?>

Posted: Thu Jan 13, 2005 3:42 pm
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

Posted: Thu Jan 13, 2005 3:42 pm
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.

return function variables

Posted: Thu Jan 13, 2005 5:20 pm
by twb
Thanks Seattlebadboy
It worked great
Thanks troy