Page 1 of 1

Problem with funtion

Posted: Thu Jul 10, 2003 12:34 am
by php_wiz_kid
What's wrong with my function:

Code: Select all

$phone_num = '';

function empty_var($var) {
    if(empty($var)) {
        $var = 'This variable is empty<br>';
    &#125;
&#125;

empty_var($phone_num);
echo $phone_num;
It doesn't echo anything. I'm new to functions so this is probably really bad, but bear with me please.

Re: Problem with funtion

Posted: Thu Jul 10, 2003 12:52 am
by nielsene
If you want a function to change a value that was passed in you have to pass the variable by reference, instead of by value. To do this you have to add an ampersand to the function header:
php_wiz_kid wrote:What's wrong with my function:

Code: Select all

$phone_num = '';
//                         -V-  look here, add the ampersand (&)
function empty_var(&$var) {
    if(empty($var)) {
        $var = 'This variable is empty<br>';
    }
}

empty_var($phone_num);
echo $phone_num;
It doesn't echo anything. I'm new to functions so this is probably really bad, but bear with me please.

Posted: Thu Jul 10, 2003 1:43 am
by php_wiz_kid
All I had to do was enter an ampersand? Thanks alot.