Search code examples
phpclassfunctionvariablespublic

PHP put null if value not sent to public function


I thought this would be easy, but some reason it is not work

public function test($data = null){

}

but when I want to send some data to the function it turns it to null so how can i have it so that if i dont send anything it is null otherwise it the data I sent


Solution

  • function test($data = null){
         echo $data;
    }
    
    test('abcd'); // output abcd
    

    see working example http://codepad.viper-7.com/Z8T23k