Similar to other programming languages, PHP programmers provide a way to define features that can be called solutions elsewhere. The syntax for a function is:
Function definition method
The code is as follows:
Function "function_name" (arg1, arg2 ...)
{
[Code to execute]
Return [final_result];
}
[Final_result] usually returns the variable value from the function.
Let's look at an example.
The code is as follows:
Function double_this_number ($ input_number)
{
Return $ input_number * 2;
}
Call method
The code is as follows:
$ X = 10;
$ Y = double_this_number ($ x );
Print $ y;
The output value is
10
Okay. let's take a look at a more complex function usage method.
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 parameter can be left unspecified because a parameter is set for $ v = 0 by default, which is helpful for function extension.
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