As AlmaDoMundo's answer shows: make the argument optional:
function myFunction($arg1, $arg2, $arg3 = null)
{
}
Then, to avoid warnings (undefined index), you'll have to call the function like so:
if (isset($_POST['example']))
{
myFunction($var1, $var2, $_POST['example']);
}
else
{
myFunction($var1, $var2);
}
If, for some reason you're unable to change the function definition (and therefore, it's signature), just call the function like this:
$var3 = isset($_POST['example']) ? $_POST['example'] : null;
myFunction($var1, $var2, $var3);//null is a value, so you're passing 3 arguments
Calling the your function the second way myFunction($var1, $var2, isset($_POST['example']));
is not the way forward. Just look at isset
's signature
bool isset ( mixed $var [, mixed $... ] )
it returns a bool
(true/false), not the value of the variable itself. If the key exists, your function will receive true
as a third argument, if not false
. You want to pass the actual value, so you'll have to either wrap your code in if/else
branches, or use a ternary (myFunction($var1, $var2, isset($_POST['example']) ? $_POST['example'] : null);
.
Personally, I'd not use a ternary here, because the first example of how to call the function I gave here is just much more readable (it's important to write maintainable code!)
On the notice:
You say you don't see any errors/warnings if you pass $_POST['example']
(you should quote the 'example'
, BTW, because that should issue a warning, too). Try changing your ini settings to:
error_reporting = E_ALL | E_STRICT
#and
display_errors = 1
display_startup_errors = 1 # careful here!
Then, you'll see how many issues your code really has. Check the manual to see what each setting does, how and when to use them.