Search code examples
phpnulldefaultuser-defined-functionsis-empty

default value assigned to an attribute of a function is not carrying it in php


I have the below code in php.

 function something($abc='abc', $bcd){
        echo $abc;
        echo $bcd;
    }
something('','Hi buddy');

Output I am getting

Hi buddy

I understand why I am getting it so, It is because the value I have sent for $abc is empty.

Is there any way in php to get the default value if the value is set empty, false, NULL, undefined

I am sorry if it looks silly. Till today I had thought that the empty value will be replaced with the default value assigned in the function.

any suggestions. thanks in advance.


Solution

  • In order to get the default results, you need to call your function like this:

    something(null, 'Hi buddy');
    

    '' is not an empty parameter, it's just an empty string.