Php function usage and function definition _ PHP Tutorial

Source: Internet
Author: User
Php function usage and function definition methods. The syntax of a function is: Copy the code of the function definition method: functionfunction_name (arg1, arg2 ...) {[codetoexecute] return [final_result];} The [final_re 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 code for defining the functions of functions is as follows: function "function_name" (arg1, arg2. ..) {[code to execute] return [final_result];} where [final_re...

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.