How does php define a function that can accept null?

Source: Internet
Author: User
Tags php define
I define a function that accepts the String parameter, but transmits a null value during the call. php will report a fatalerror, but the actual situation will indeed result in a null parameter, I want to define a function that specifies the parameter type and can accept null. what should I do? {Code ...} execute... I define a function that accepts the String parameter, but transmits a null value during the call. php will report a fatal error, but the actual situation will indeed result in a null parameter, I want to define a function that specifies the parameter type and can accept null. what should I do?

 

Run the following command:

>php t.phpCatchable fatal error: Argument 1 passed to foo() must be an instance of String, null given, called in /Users/jiajun/t.php on line 7 and defined in /Users/jiajun/t.php on line 3

Reply content:

I define a function that accepts the String parameter, but transmits a null value during the call. php will report a fatal error, but the actual situation will indeed result in a null parameter, I want to define a function that specifies the parameter type and can accept null. what should I do?

  

Run the following command:

>php t.phpCatchable fatal error: Argument 1 passed to foo() must be an instance of String, null given, called in /Users/jiajun/t.php on line 7 and defined in /Users/jiajun/t.php on line 3

If you do not provide a parameter, the default value is null.

                                           

Remove the String modifier.

To determine the parameter type, use is_string ($ arg.

Http://www.php.net/manual/en/language...

If NULL is used as the default parameter value, it will be allowed as an argument for any later call

The manual has already been quite clear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.