Quick Fix (3)-PHP: function basis, function parameters, function return value, variable function, anonymous function, closure function, callback function

Source: Internet
Author: User

[SOURCE DOWNLOAD]


Quick Fix (3)-PHP: function basis, function parameters, function return value, variable function, anonymous function, closure function, callback function



Webabcd


Introduced
Quick and fast PHP

    • Function basics
    • function parameters
    • function return value
    • Variable functions
    • anonymous functions
    • Closure function
    • callback function



Example
1, the function of the relevant Knowledge point 1 (Basic)
function/function1.php

<?PHP/** * Function Related Knowledge point 1 (Basic)*///The function can be called before the associated function declaration statementF1 ();functionF1 () {Echo"F1"; Echo"<br/>";}//here call F2 () on the wrong//F2 ();$b=true;if($b){    functionF2 ()//This function is declared when the condition is established    {        Echo"F2"; Echo"<br/>"; }}f2 ();//It is wrong to call F4 () here (because F4 's declaration is inside the F3, so call F3 before calling F4)//F4 ();f3 (); F4 ();functionF3 () {Echo"F3"; Echo"<br/>"; functionf4 () {Echo"F4"; Echo"<br/>"; }}


2, the function of the relevant Knowledge point 2 (function parameters)
function/function2.php

<?PHP/** * Function Related Knowledge point 2 (function parameter)*///functions with ParametersF1 ("WEBABCD");functionF1 ($name){    Echo"Hello".$name;//Output:hello WEBABCD    Echo"<br/>";}//function with parameters (argument is reference type)$name= "WEBABCD"; F2 ($name);functionF2 (&$n)//parameter is a reference type{    Echo"Hello".$n;//Output:hello WEBABCD    Echo"<br/>"; $n= "Wanglei";//$n is a reference type, which is the same as a global variable $name point}Echo"Hello".$name;//Output:hello WangleiEcho"<br/>";//functions with parameters (parameters can specify default values)F3 ("Hello");//Output:hello WEBABCDF3 ("Hello", "Wanglei");//Output:hello WangleifunctionF3 ($p 1,$p 2= "WEBABCD")//the default value of a parameter can only be a constant, not an expression{    Echo $p 1.$p 2; Echo"<br/>";}


3, the function of the relevant Knowledge point 3 (function return value)
function/function3.php

<?PHP/** * Function Related Knowledge point 3 (function return value)*///function with return value$result 1= F1 ("WEBABCD");Echo $result 1;Echo"<br/>";functionF1 ($name){    return"Hello".$name;//Output:hello WEBABCD}//return from function with a reference (both the Declaration and the invocation of the function are added &)$result 2= &F2 ();Echo $result 2;//output:1Echo"<br/>";$result 2= 100;//$result 2 is a reference that points to what the $i points to, so the value $i after this sentence is$result 3= &F2 ();Echo $result 3;//output:101Echo"<br/>";function&AMP;F2 ()//This function returns a reference (both the Declaration and the invocation of the function are added &){    Static $i= 0; $i++; return $i;}


4, the function of the relevant knowledge point 4 (variable function, anonymous function, closure function)
function/function4.php

<?PHP/** * Function Related knowledge point 4 (variable function, anonymous function, closure function)*///variable function (variable), the value of the variable as the function namefunctionF1 () {Echo"F1";//output:f1    Echo"<br/>";}functionF2 () {Echo"F2";//Output:f2    Echo"<br/>";}$func= ' F1 ';$func();$func= ' F2 ';$func();//Anonymous FunctionsEcho Preg_replace_callback //Output:username(    ' ~-([A-z]) ~ ',function($match)//Anonymous Functions    {        return Strtoupper($match[1]); }, ' User-name ');//closure Function//$f 3 ("WEBABCD");//This is wrong, for a closure function, the call is made after the declaration statement$f 3=function($name){    Echo"Hello".$name;//Output:hello WEBABCD    Echo"<br/>";}; //don't forget the ";" of the closure function .$f 3("WEBABCD");


5, the function of the relevant Knowledge point 5 (callback function)
function/function5.php

<?PHP/** * Function Related Knowledge point 5 (callback function)*///used to demonstrate how to callback (callback) functionsfunctionmycallbackfunction () {return' Mycallbackfunction ';}classmyclass1{//used to demonstrate how to callback (callback) instance methods     Public functionmyMethod1 () {Echo' MyClass1 myMethod1 '; Echo' <br/> '; }    //used to demonstrate how to callback (callback) class methods     Public Static functionmyMethod2 () {Echo' MyClass1 myMethod2 '; Echo' <br/> '; }}//calling the specified function via Call_user_funcEcho Call_user_func(' Mycallbackfunction ');Echo' <br/> ';$obj 1=NewMyClass1 ();//to invoke the specified method of the specified object via Call_user_funcCall_user_func(Array($obj 1, ' MyMethod1 '));//to invoke the class method of a class by means of Call_user_funcCall_user_func(Array(' MyClass1 ', ' myMethod2 '));//to invoke the class method of a class by Call_user_func (PHP 5.2.3 or above)Call_user_func(' myclass1::mymethod2 ');



Ok
[SOURCE DOWNLOAD]

Quick Fix (3)-PHP: function basis, function parameters, function return value, variable function, anonymous function, closure function, callback 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.