Home >Backend Development >PHP Tutorial >PHP function usage method and function definition method_PHP tutorial

PHP function usage method and function definition method_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:101161browse

The syntax for a function is:
Function definition method

Copy code The code is as follows:

function "function_name" ( arg1, arg2...)
{
[code to execute]
return [final_result];
}

where [final_result] is usually a variable returned from a function value.
Let’s see an example
Copy the code The code is as follows:

function double_this_number($input_number)
{
return $input_number*2;
}

Call method
Copy code The code is as follows:

$x = 10;
$y = double_this_number($x);
print $y;

The output value is
10
Good , let’s look at a more complicated function usage method
Copy code The code is as follows:

function safePost($v=0 )
{
if( $v==0 )
{
$protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV ", "GLOBALS");
foreach($protected as $var) {
if(isset($_REQUEST[$var]) || isset($_FILES[$var]))
{
die("Access denied");
}
}
}
}

Call method
safePost();
This is optional Define parameters, because $v==0 is set with a parameter by default, which is very helpful for function expansion.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321825.htmlTechArticleThe syntax for a function is: Function definition method copy code The code is as follows: function "function_name" (arg1, arg2. ..) { [code to execute] return [final_result]; } where [final_re...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn