Quick bi (3)-PHP: Function Basics, function parameters, function return values, variable functions, anonymous functions, closure functions, callback functions-webabcd [source code download]
Quick Decision (3)-PHP: Function Basics, function parameters, function return values, variable functions, anonymous functions, closure functions, and callback functions
Author: webabcd
Introduction
PHP
- Function Basics
- Function parameters
- Function return value
- Variable functions
- Anonymous functions
- Closure functions
- Callback function
Example
1. knowledge about functions 1 (basic)
Function/function1.php
";}// When f2 () is called here, the error is returned. // f2 (); $ B = true; if ($ B) {function f2 () // This function {echo "f2"; echo "is declared only when the condition is set"
";}} F2 (); // it is wrong to call f4 () here (because the f4 statement is inside f3, you must call f3 before calling f4) // f4 (); f3 (); f4 (); function f3 () {echo "f3"; echo"
"; Function f4 () {echo" f4 "; echo"
";}}
2. knowledge about Functions 2 (function parameters)
Function/function2.php
";}// A function with parameters (the parameter is of the reference type) $ name =" webabcd "; f2 ($ name); function f2 (& $ n) // the parameter is of reference type {echo "hello ". $ n; // output: hello webabcd echo"
"; $ N =" wanglei "; // $ n is a reference type and the content pointed to by the global variable $ name is the same.} echo" hello ". $ name; // output: hello wangleiecho"
"; // A function with parameters (the default value can be specified for the parameter) f3 (" hello "); // output: hello webabcdf3 (" hello "," wanglei "); // output: hello wangleifunction f3 ($ p1, $ p2 = "webabcd") // The default value of the parameter can only be a constant, not an expression {echo $ p1. $ p2; echo"
";}
3. knowledge about functions 3 (function return value)
Function/function3.php
"; Function f1 ($ name) {return" hello ". $ name; // output: hello webabcd} // A reference is returned from the function. (the &) $ result2 = & f2 () is required for both function declaration and call (); echo $ result2; // output: 1 echo"
"; $ Result2 = 100; // $ result2 is a reference pointing to the content pointed to by $ I. Therefore, the value of $ I after this sentence is 100 $ result3 = & f2 (); echo $ result3; // output: 101 echo"
"; Function & f2 () // This function returns a reference (the declaration and call of the function must be added &) {static $ I = 0; $ I ++; return $ I ;}
4. knowledge about functions 4 (variable functions, anonymous functions, and closure functions)
Function/function4.php
";} Function f2 () {echo" f2 "; // output: f2 echo"
";}$ Func = 'F1'; $ func (); $ func = 'F2'; $ func (); // Anonymous function echo preg_replace_callback // output: userName ('~ -([A-z]) ~ ', Function ($ match) // Anonymous function {return strtoupper ($ match [1]) ;}, 'user-name '); // closure function // $ f3 ("webabcd"); // This is incorrect. for the closure function, $ f3 = function ($ name) {echo "hello ". $ name; // output: hello webabcd echo"
";}; // Do not forget the"; "$ f3 (" webabcd ") of the closure function ");
5. knowledge about functions 5 (callback function)
Function/function5.php
';} // Used to demonstrate how to call back the class method public static function myMethod2 () {echo 'myclass1 myMethod2'; echo'
';}} // Call the specified function echo call_user_func ('mycallbackfunction') through call_user_func; echo'
'; $ Obj1 = new MyClass1 (); // call the specified method call_user_func (array ($ obj1, 'mymethod1') of the specified object through call_user_func ')); // call the class method call_user_func (array ('myclass1', 'mymethod2 ') of the class through call_user_func; // call the class method of the class through call_user_func (PHP 5.2.3 or above) call_user_func ('myclass1: myMethod2 ');
OK
[Download source code]