Sending default parameter as null and use default value set in function

Online

I have function as below.

function test($username, $is_active=1, $sent_email=1, $sent_sms=1) {

  echo $sent_email;  // It should print default ie 1

}

Call to function :

  test($username, 1, null, 1);

How to call function if need to use default value in function. $sent_email should be 1. Can not change sequence of parameter.

Frank Núñez Rodríguez

When you start the value parameters happens:

 function makecoffee($type = "cappuccino")
    {
        return "Making a cup of $type.\n";
    }
    echo makecoffee();
    echo makecoffee(null);
    echo makecoffee("espresso");
    ?>

The above example will output:

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.

To fulfill what you want to check with conditions as follows:

function test($username, $is_active=1, $sent_email=1, $sent_sms=1) {

    if($sent_email!=1)
        $sent_email=1;
      echo $sent_email;  // It should print default ie 1

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set a default value for a parameter if null?

Set the default value for the key of the function object parameter

Set a default parameter value for a JavaScript function

Which default value to use for an optional parameter that is a function

Default value for function as parameter

Is there a way to make a Powershell function ignore a default parameter's value if its parameter set is not in use?

Set function default this value

How to set value for specific default value of a parameter in a javascript function

How to set default value of date input parameter in procedure if they are null?

using the default value if the parameter is null

Dart set default value for parameter

Set default value and type for parameter

if a value is null, then use a default value

Cannot use null as default value for parameter in PHP-8

Can swift closures be set to a default value when used as a parameter in a function?

How to set a default value for an optional positional parameter of type Function?

Set default value for parameter of List<string> type in function

Python: How to set the default value when the parameter type is function?

get function parameter with default value

JavaScript function default parameter value

Set default value but result is null

Set default value for NULL in datatable

Set default value if null in twig

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

Use initializer list as default value for function/method parameter

It is possible to use default parameter value its same function name

Is It Possible To Set Default Parameter Value On A Rest Parameter

Passing null to an optional parameter with default value null

How to set default value in function