jackeduphard
jackeduphard

Reputation: 42

in a function can you retun a null to exit the function PHP

I am passing the two var's by ref to change them, and once I have changed them or one of the (8 loops) have found a positive in the string I am using, I want to exit the function, but I don't need to return anything because they are passed by ref.

I could just pass a copy of one and then ref the other one and set the var of the one that is copied = to the function and return that, but is there a cleaner way where I just call the function, the vars are set and I can move on?

function get_cat_size($urlstr, &$cat, &$size){ return null; };

$cat = get_cat_size($urlstr, &$size);

Does the first one work or not? Witch is better for readability?

Thanks for the input!

while( $i < $countz )
    {
    $pos = strpos($outdoor, $outdoor[$i]);
    if($pos != false)
    {
        $cat = $outdoorID;
            while( $j < $sizeArrayCount)
            {
            $poz = strpos($outdoor, $outdoor[$i]);
            if($poz != false)
            {
                $size = $outdoorID;
                return;
            }
            $j++;
            }
        return;
    }
    $i++;
    }

^ so this should work yes no maybe so?

So this is one of 8 loops set up in a order because they are least important to important, with different var = different stores.

Upvotes: 0

Views: 350

Answers (2)

Anthony Forloney
Anthony Forloney

Reputation: 91806

Take a look at break for returning out of your for loops.

I would personally avoid returning null within a function since NULL will be returned by default when there is no return value specified. You can read more here at PHP: Returning values

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158100

You can just return without a value:

function returnsNothing (&$a, &$b) {
    return;
}

Or simpler just omit the return statement at all

function returnsNothing (&$a, &$b) {
    // do something
}

Both snippets will make the function returning NULL.

Upvotes: 3

Related Questions