PHP function Detailed learning Note _php Tutorial

Source: Internet
Author: User
In PHP function is commonly used a reasonable reusable method, the function of the system functions and user-defined functions, the following I will give you the use of these two PHP functions, there is a need to understand the friends can refer to.

System functions

such as Phpinfo (); This is a system function, about system functions we do not say, direct reference to PHP tutorials or manuals will be.

User-defined Functions

The concept of introducing a function into PHP makes the program concise and computationally efficient. A function is a program that implements a function that is equivalent to a module that can be invoked with some simple commands, without having to be rewritten every time. Just like a computer, we don't need to know the complex structure inside, as long as we understand the functions it provides directly.

PHP Custom Function-php Although there are many useful functions built in, but it is not possible to meet the needs of the actual design, then we need to create their own functions.

Basic structure form:

The code is as follows Copy Code

Function name (parameter) {
Statement body
}

function is the declaration functions;

Function names must be unique, unlike variable names, where the function names are not case-sensitive;
There must be a space between function and function name;
Parameters can be omitted, or multiple parameters can be added, separated by commas.
PHP Call Function-Once we have defined a function, we can call the function in the following structure:

Function name (parameter)

Arguments can be omitted, but parentheses must be preserved.
Instance:

The code is as follows Copy Code

function e () {
$i =5+6;
echo $i;
}
E ();
?>

Passing parameters of a function

If a function defines a parameter, you must pay attention to the passing of the parameter value when calling the function. PHP function parameters are passed in two ways: by value and by reference, by default, by value.

1. Pass by value:

Example 1:

The code is as follows Copy Code

function sum ($num, $price) {
$total = $num * $price;
echo "Total Price is: $total";
}
SUM (10,78);
?>

Default parameters

PHP function Default parameter-If a function does not provide a value for the parameter, the parameter will use the default value that the function has made, and the default value must already be assigned to the function definition.

Instance:

The code is as follows Copy Code

function add ($a, $b =10) {/* define variable $b default parameter */
$sum = $a + $b;
echo $sum. "
";
}
Add (1,5); /* Replace default parameter by value pass parameter */
Add (1); /* No parameters are passed to $b with default values */
?>

function return value

When we design the program, many times we do not want to run the results of the function directly in the browser display, you can use the return statement to first call out the function value and other operations.

PHP function return value instance:

The code is as follows Copy Code

function add ($a, $b =10) {
$sum = $a + $b;
return $sum; /* Assign the value of $sum to the function */
}
$sum =add (1,5); /* Here the variable $sum gets the value of the function */
echo $sum. "
";
?>

function nested call

The inside of a function can be either a generic execution statement or a function, which is the use of nested PHP functions.

function nested only one function inside also called other functions, here to understand is that each function is in a parallel relationship, the definition of function is independent. It's like a container where you can put a lot of small containers, and these containers are independent of each other and hold their own things.

Instance:

The code is as follows Copy Code

function Volume ($l, $w) {
function height ($a) {
$a = $a/2;
return $a;
}
$a = $l * $W;
$v = $a *height ($a);
Echo $v;
}
Volume (8,3);
?>

function recursive invocation

What is a php recursive function? Before explaining this concept, let's look at a mathematical example. If you want to calculate the factorial of N, that is n!, you should first know (n-1)!, and to Know (n-1)!, and must Know (n-2)!. And so on, until 1!=1, and then return to calculate 2!,3!... (n-1)!,n!, the result of n factorial can be finally obtained.

The meaning of recursive invocation of PHP functions is that functions can invoke itself, and this direct or introductory operation of invoking the function itself is allowed in the PHP language, as a recursive function.

Instance:

The code is as follows Copy Code

function Calculate ($n) {
if ($n ==0)
return 1;
Else
return $n *calculate ($n-1);
}
$s =calculate (3);
Echo $s;
?>

http://www.bkjia.com/PHPjc/628827.html www.bkjia.com true http://www.bkjia.com/PHPjc/628827.html techarticle in PHP, functions are commonly used as a reasonable reusable method, the function of the system functions and user-defined functions, the following I would like to introduce the two methods of using PHP functions ...

  • 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.