Search code examples
phpfunctionrecordset

PHP newbie question: return variable with function?


i've been coding asp and was wondering if this is possible in php:

$data = getData($isEOF);

function getData($isEOF=false)
{
    // fetching data
    $isEOF = true;
    return $data;
}

the function getData will return some data, but i'd like to know - is it possible to also set the $isEOF variable inside the function so that it can be accessed from outside the function?

thanks


Solution

  • It is possible, if your function expects it to be passed by reference :

    function getData(& $isEOF=false) {
    
    }
    

    Note the & before the variable's name, in the parameters list of the function.


    For more informations, here's the relevant section of the PHP manual : Making arguments be passed by reference


    And, for a quick demonstration, consider the following portion of code :

    $value = 'hello';
    echo "Initial value : $value<br />";
    
    test($value);
    echo "New value : $value<br />";
    
    
    function test(& $param) {
        $param = 'plop';
    }
    

    Which will display the following result :

    Initial value : hello
    New value : plop