Some cold knowledge about PHP Functions

Source: Internet
Author: User
Tags addall

Function nesting

A nested function can be defined in the function in PHP, which is different from other languages such as C. Nested functions are valid only after the peripheral functions are run. That is to say, the execution of peripheral functions allows the system to redefine a new function.

<? Phpfunction a () {function B () {echo "function B <br>" ;}echo "function a <br>" ;}// B (); // cannot run. a (); B ();?> is not defined.

Run B () at the beginning (). An error is reported because the system cannot find function B. However, after running function a, the statement defining function B in function a also runs. Therefore, the system has function B. Therefore, you can run function B.

The running result is:

Function
Function B

Global variable

The use of this keyword should be familiar with php development. Before using global variables in a function, you must add global labels. In fact, it can be understood that the global variable is an alias of the external global variable of the function. Using the global keyword is equivalent to creating a reference to the variable in the $ GLOBALS variable.

<?php$var=100;function a(){   global $var;   $var=$var+1;   echo $var."<br/>";}a();echo $var."<br/>";?>

It is equivalent to the following statement:

<?php$var=100;function a(){   $GLOBALS["var"]=$GLOBALS["var"]+1;   echo $GLOBALS["var"]."<br/>";}a();echo $var."<br/>";?>

The final output is 101 101.

Static variables

Unlike C #, PHP functions allow static variables, which are used in a similar way as static variables in the class.

Default and variable parameters

Similar to C #, PHP functions also support default and variable parameters. Similar to C.

function a($a,$b=100){echo ($a+$b)."<BR>";}a(100,10);a(100);

PHP functions sometimes require an indefinite parameter. In this case, you only need to leave the parameter of the function empty. In PHP, when a function is called, there are more input parameters than the defined parameters, and no error is reported. In PHP, only the required parameters are obtained in order. If PHP inputs less parameters than the preset ones, an error is returned. In PHP, you can use the following three built-in methods to obtain the parameter information.

$array = func_get_args();$count = func_num_args();$value = func_get_arg(argument_number);

Let's look at the example.

<? Phpfunction addall () {$ array = func_get_args (); echo "parameter array:"; print_r ($ array); echo "<br>"; $ count = func_num_args (); echo "parameter count:"; print ($ count); echo "<br>"; $ sum = 0; for ($ I = 0; $ I <$ count; $ I ++) {$ value = func_get_arg ($ I); echo "the value of the $ I parameter is $ value <br>"; $ sum + = $ value ;} echo "sum $ sum"; echo "<br>" ;}addall (, 10, 20, 13) ;?>

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" CA43B887143048948DD3075EB80CB7E8 "border =" 0 "alt =" CA43B887143048948DD3075EB80CB7E8 "src =" http://www.bkjia.com/uploads/allimg/131228/11264Hc9-0.jpg "height =" 118 "/>

Type suggestion

By default, you do not need to know the type of the variable in the PHP function. However, in some cases, some functions only accept specific types. We need to take type hint for the parameter of this function.

The syntax is similar to the C # definition function. You can specify the parameter type before the parameter. See the following example:

<? Phpclass A {} class B extends A {} class C {} function test (A $ a, B $ B) {} test (new a, new B ); // you can run test (new B, new B); // you can run test (new a, new a); // Error test (new a, new c ); // error?>

If function test is defined as function test ($ a, $ B) {}, the following four call methods can be run.

However, once the parameter type is specified, the last two calls will fail because they do not match the parameter type.

Variable Functions

Similar to variable variables, function names can also be represented by variables.

<? Phpfunction fa () {echo "fa running... <br> ";} function fb () {echo" fb running... <br> ";}$ f =" fa "; $ f (); // equivalent to calling fa () $ f =" fb "; $ f (); // equivalent to calling fb ()?>

Anonymous Functions

Like the concept of anonymous functions in C #, PHP also has the concept of anonymous functions. PHP supports creating functions by using built-in functions. According to the example in the php manual,

<?php $newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');  echo "New anonymous function: $newfunc\n";  echo $newfunc(2, M_E) . "\n";  // outputs  // New anonymous function: lambda_1  // ln(2) + ln(2.718281828459) = 1.6931471805599  ?>

$ Newfunc is similar to the delegate type in c #. It can call the created function.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1282642

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.