ipel
ipel

Reputation: 1348

php passing a variabile that is not always set in a user function

i have created a simple function in php, but i'm not sure about how to pass a (optional) variabile if this is not always set, for example:

myfunction( $var1, $var2, $_POST[example] );

as you can see the myfunction is always called but the $_POST[example] isset/exist only when the form was already sent.

my question is, is that correct??? if $_POST[example] don't exist i see no error/notice (Notice: Undefined variable..) but i'm not sure...

maybe is better like this?

 myfunction( $var1, $var2, isset($_POST[example]) );

Upvotes: 0

Views: 87

Answers (5)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76413

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.

Upvotes: 0

Alma Do
Alma Do

Reputation: 37365

Declare it as:

function myfunction( $var1, $var2, $var3=null )
{
}

you can read more about function parameters here (see Example#3).

As for your second question (about notice) - you have to check your $_POST first:

if(isset($_POST['example']))
{
   myfunction($var1, $var2, $_POST['example']);
}
else
{
   myfunction($var1, $var2);
}

or, alternatively:

myfunction($var1, $var2, isset($_POST['example'])?$_POST['example']:null);

in that case you will hanlde your data correctly and error will be not shown due to that (and not due to errors suppressing).

Upvotes: 1

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

You can try something like this.During the function definition declare the argument as an optional one.

class test{
public function myFun($arg1,$arg2,$arg3 = ''){
echo 'Argument 1 - '.$arg1;
echo 'Argument 2 - '.$arg2;
if($arg3!= '')
   echo 'Argument 3 - '.$arg3;
}
}
 $obj = new test();
 echo $obj->myFun('One','Two');

Upvotes: 0

Girish Thimmegowda
Girish Thimmegowda

Reputation: 2169

First Case

if your form has not submitted before, then the third argument will have the value null else the value submitted.

Second case

If your form has not submitted before then the third argument will have the value false, else true.

Upvotes: 0

Bora
Bora

Reputation: 10717

Equal to NULL or false

myfunction($var1, $var2, $_POST['example']);

function myfunction($var1, $var2, $example = false)
{
    if($example) {
       echo $example;
    }
}

Upvotes: 1

Related Questions