Viewed   75 times

In PHP, if I have a function written like this

function example($argument1, $argument2="", $argument3="")

And I can call this function in other place like this


But if I want to give some value to the thrid argument, Should I do this

example($argument1, "", "test");


 example($argument1, NULL, "test");

I think both will work but what is the better way? Because in future if the value for the $argument2 is changed in the main function and if I have "", then will there be any problem? Or NULL is the best option?




You can use either:

example($argument1, '', 'test');


example($argument1, NULL, 'test');

You can always check for NULL using instead of empty string:

if ($argument === NULL) {

I think it all depends on what happens inside the function with the args.

Tuesday, December 27, 2022

You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.

You can, however, declare a variadic function that takes in a variable number of arguments. You would use func_num_args() and func_get_arg() to get the arguments passed, and use them normally.

For example:

function myFunc() {
    for ($i = 0; $i < func_num_args(); $i++) {
        printf("Argument %d: %sn", $i, func_get_arg($i));

Argument 0: a
Argument 1: 2
Argument 2: 3.5
myFunc('a', 2, 3.5);
Sunday, August 21, 2022

You described the situation rather well. There is no other way I am aware off and that is standard conforming. The pattern with a local variable named similarly is what people often use. The other option is to just put if (present()) else everywhere, but that is awkward.

The point is that they are optional arguments, not default arguments. Fortran doesn't have default arguments. The may have been better, but that is not what the committee members have chosen in the 80s when preparing Fortran 90.

Saturday, September 3, 2022

C++98 §12.1/5 (emphasis mine):

A default constructor for a class X is a constructor of X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared.

So yes, it does count as a default constructor. See also.

Wednesday, August 3, 2022

Use debug_backtrace instead. It will give you the whole trace and doesn't trim arguments as far as I know.

On a second thought: You might get away with it by using

try {
} catch (Exception $e)


Tuesday, November 8, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :