On the setting rules of parameters of PHP function

Source: Internet
Author: User

The function can either set parameters or leave the setting blank.

1. When the PHP function sets the parameter, but the reference argument is not given, the situation is as follows:
1<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title>php function Test </title>67 8<body>9<?PHPTen /********************* One *author: Blind cat Meets Dead Tiger * * * A *date:2/6/2015******** - *********************/ -  the functionasd$a,$b,$c){ -     Echo $a+$b+$c; - } -  +asd); -  +  A?> at</body> -

The result is:

1warning:missing Argument 1 forASD (), called in F:\phpfind\WWW\ceshi.php on line anddefinedIn F:\phpfind\WWW\ceshi.php on line 152 3Warning:missing Argument 2 forASD (), called in F:\phpfind\WWW\ceshi.php on line anddefinedIn F:\phpfind\WWW\ceshi.php on line 154 5Warning:missing Argument 3 forASD (), called in F:\phpfind\WWW\ceshi.php on line anddefinedIn F:\phpfind\WWW\ceshi.php on line 156 7notice:undefined variable:b in F:\phpfind\WWW\ceshi.php on line 168 9notice:undefined variable:a in F:\phpfind\WWW\ceshi.php on line 16Ten  Onenotice:undefined variable:c in F:\phpfind\WWW\ceshi.php on line 16 A0

Conclusion: There will be an error, but it can also show the results. to 0.

2. When a function is a physical parameter, the first argument is not separated by commas, and the argument is followed by a reference. This time the syntax is wrong. This notation is not supported in PHP. If you want to do this, you can give null. Examples are as follows:
1<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title>php function Test </title>67 8<body>9<?PHPTen /********************* One *author: Blind cat Meets Dead Tiger * * * A *date:2/6/2015******** - *********************/ -  the functionasd$a,$b,$c){ -     Echo $a+$b+$c; - } -  +asdNULL, 3,7); -  +  A?> at</body> -

The result is:

1 10

3. Now for the test parameter setting the default parameters, the situation is as follows:
1<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title>php function Test </title>67 8<body>9<?PHPTen /********************* One *author: Blind cat Meets Dead Tiger * * * A *date:2/6/2015******** - *********************/ -  the functionasd$a,$b=2,$c=5){ -     Echo $a+$b+$c; - } -  +ASD (3,7); -  +  A?> at</body> -

The result is:

1 15

Conclusion: If there is a default parameter, the argument can not be given. The order of the arguments is in the order of the formal parameters. If there is a default parameter after the argument in the function argument, the argument can be given without the corresponding parameter. The function will be evaluated by default parameters.

4. Let's look at one more situation. At this time three parameters, the default parameter is set in the middle. If two arguments are given, what is the situation like?
1<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title>php function Test </title>67 8<body>9<?PHPTen /********************* One *author: Blind cat Meets Dead Tiger * * * A *date:2/6/2015******** - *********************/ -  the functionasd$a,$b=2,$c){ -     Echo $a+$b+$c; - } -  +ASD (8,9); -  +  A?> at</body> -

The result is:

1  for defined in F:\phpfind\WWW\ceshi.php23 notice:undefined variable:c in F:\phpfind\W ww\ceshi.php on line4 17

Conclusion: When referencing a function, the order of the arguments corresponds to the parameter one by one. If there is no default parameter, the corresponding argument is not given an error. When there is a default parameter behind a function's argument group, the omission is not an error at this time, and the function will participate in the operation by default parameters.

5. Now look at the case where the parameter is set to null and the argument is empty.
1<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title>php function Test </title>67 8<body>9<?PHPTen /********************* One *author: Blind cat Meets Dead Tiger * * * A *date:2/6/2015******** - *********************/ -  the functionasd$a=NULL,$b=2,$c=9){ -     Echo $a+$b+$c; - } -  + ASD (); -  +  A?> at</body> -

The result is:

1 11

Conclusion: The formal parameter is set to NULL, and the default argument is set relative to it. This time will not be an error.

6. The parameter is null and the argument has a numeric value.
1<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title>php function Test </title>67 8<body>9<?PHPTen /********************* One *author: Blind cat Meets Dead Tiger * * * A *date:2/6/2015******** - *********************/ -  the functionasd$a=NULL,$b=2,$c=9){ -     Echo $a+$b+$c; - } -  +ASD (9,9); -  +  A?> at</body> -

Results:

1 27

Conclusion: Obviously, this time the function is in accordance with the order of the formal parameters of the operation.

On the setting rules of parameters of PHP function

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.