The best answer I can give is that you shouldn't be trying to rely on whether or not something got set, but instead structure your code so that it is always set.
Just saw your edit.
A function parameter can have a default value, so that you know it is always set. function myFun($variable = null)
will ensure that accessing $variable will never throw an exception.
Edit: If you really need to know if a parameter was passed in or not, use func_get_args(). It returns an array, and you can check its size.
function foo($bar) {
var_dump(func_get_args());
}
foo();
foo('foo');
gives the output:
array(0) {
}
array(1) {
[0] =>
string(3) "foo"
}
BTW though, calling foo() with no arguments emits an error:
PHP Warning: Missing argument 1 for foo(), called in /Users/pittsd/dev/cbsi/fly/cnet/expirement/expirement.php on line 6 and defined in /Users/pittsd/dev/cbsi/fly/cnet/expirement/expirement.php on line 2
Warning: Missing argument 1 for foo(), called in /Users/pittsd/dev/cbsi/fly/cnet/expirement/expirement.php on line 6 and defined in /Users/pittsd/dev/cbsi/fly/cnet/expirement/expirement.php on line 2
To further answer your question:
<?php
function foo() {
if (func_get_args()) {
$value = func_get_args()[0];
if ($value === null) {
echo "Have a null value. ". PHP_EOL;
} else {
echo "Have a value: ".$value. PHP_EOL;
}
}
else {
echo "No value passed in.". PHP_EOL;
}
}
foo(null);
foo('foo');
foo();
outputs:
Have a null value.
Have a value: foo
No value passed in.