PHP’s isset() function

I did a quick experiment with the isset function in PHP that I thought might be useful to others (as well as myself down the road). I was curious as to how ‘isset’ acted when tested against function arguments that do not have default values set in the function definition.

Here is the code:

<?php

function one_arg($one) {
    return (isset($one)) ? 'true' : 'false';
}

class one_arg_class {
    public function one_arg($one) {
        return (isset($one)) ? 'true' : 'false';
    }
}

echo one_arg() . "\n";
echo one_arg(0) . "\n";

echo one_arg_class::one_arg() . "\n";
echo one_arg_class::one_arg(0) . "\n";

$object = new one_arg_class();
echo $object->one_arg() . "\n";
echo $object->one_arg(0) . "\n";

?>

The output came back:
false
true
false
true
false
true

There were warnings produced by the above code as well. I left them out for readability.

The the results show that no matter how you call a function; whether it be globally, or through a class by scope resolution or object declaration; if you do not specify a value to an argument that does not have a default value, ‘isset’ will return false.

Leave a Reply

{
}