php function declaration

Scope of php variables 

    Local variables: Variables declared in a function are local variables and can only be used within this function.

    Global variables: declared outside the function, after the variable declaration, can only be used until the entire script is settled, including in the function and {} can be used 

    The variables of php cannot be declared or used. To use a global variable in php, the global variable must be included in the function through the global keyword before it can be used. After the global, the global variable is used.

    A parameter is a local variable, and this local variable can be assigned in electrophoresis


php static variable

    Static variable values ​​can be declared in functions (classes), but not globally. Use static before variables;

    Role: A variable can be used in multiple calls through a function.

  1. Static variables are kept in static code
  2. A function is more directly shared by electrophoresis multiple times, but it can only be declared to memory when the function is called for the first time, and it will not be declared in the subsequent calls, but the variable function will be used directly.

Variable function

If a variable is a bit buckled after $var=test; $var(); is about finding a function test() with the same name as the variable value;

$ var = test;
    function test($a,$b){
    	return $a+$b;
    }
    echo "结果".$var(2,4)."<br>";

function type

PHP provides more than 2,000 functions, all of which are system functions and can be used directly by name. Be sure to use the system function first. If the system function does not have the function you want, define the function directly.

1. Regular functions

    bool copy(string source,string dest)

2. With mixed, mixed means that any type of value can be passed

    bool chown (string filename,mixed user)

3. A function with & parameter means reference assignment. This parameter cannot pass a value, and then the function changes the value of the variable. When we use this variable, the value also changes.

    bool arsort(array &array[,int sort_flags ])

4. For functions with [ ], the value of the table is optional. If you pass a value, use the value you passed. If you do not pass a value, use the default value. The default value is assigned directly when the function is declared. Values ​​must be set from back to front

    bool arsort (array &array [, int sort_flags])

function demo($a,$b=10,$c=30){
    echo "$a $b $c"
}
demo(8);

5. A parameter function with ... means that any number of parameters can be worn

    int array_unshift(array &array,mixed var [,mixed ...])

function demo(){
    $args = func_get_args();//Get the array of parameters and return the number of arrays
    $sum = 0;
    for ($i=0; $i < count($args); $i++) { //count can use func_get_args directly
    	$sum+=$args[$i];//args can be replaced with func_get_arg
    }
    return $sum;
}
echo demo(1,3,5,7,8);

6. The callback function has a callback, that is, when using this function, we need to pass a function in (function name, function name string)

    array array-filter(array input [,callback callback])

function huidiao($a,$b,$fun){
    return $a+$b+$fun($a,$b);
}
function fun($a,$b){
    return $a*$b;
}
echo "Callback function===".huidiao(2,3,fun)."<br>";

use of functions

1. Internal function: PHP can re-declare the function inside the function. The purpose is to call it inside the function, which is used to help the external function complete some sub-functions.

2. Recursive function: that is to call your own function name within yourself

function total($dirname,&$dirnum,&$filenum){
    	$dir = opendir($dirname);//Get directory file object
    	readdir($dir);//Read the first file in the directory
    	readdir($dir);
    	while ($filename=readdir($dir)) {
    		echo $filename."<br>";
    		$newFile = $dirname."/".$filename;
    		if (is_dir($newFile)) {//Determine whether it is a directory
    			total($newFile,$dirnum,$filenum);
    			$ dirnum ++;
    		}else{
    			$ filenum ++;
    		}
    	}
    	closedir($dir);//Close the directory
    }
    $ dirnum = 0;
    $ filenum = 0;
    total("/Users/luoyaosheng/Sites",$dirnum,$filenum);
    echo "Number of directories: $dirnum Number of files: $filenum";

3. Reuse function: use your own defined function library, you can include html, text, php files

require : for static includes

include : for dynamic inclusion

require_once : Even if the inclusion of once is included, it will only be counted once to prevent an error from being reported. It is normal to use without once

include_once


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525350&siteId=291194637