Learning notes for php functions-PHP Tutorial

Source: Internet
Author: User
Learning notes for php functions. In php, functions are commonly used as a reasonable method that can be used repeatedly. functions are system functions and user-defined functions, respectively, next I will introduce you to the usage of these two php functions. in php, functions are commonly used as a reasonable method that can be reused, the functions are system functions and user-defined functions. next I will introduce you to the usage of these two php functions. For more information, see.

System functions

For example, phpinfo (); this is a system function. For more information about system functions, see The php Tutorial or manual.

User-defined functions

Php introduces the function concept to make the program concise and faster. A function is a program that implements a certain function. this program is equivalent to a module. you can use some simple commands to call it. you don't have to rewrite it every time. Just like a computer, we don't need to know the complex structure in it. we just need to know the functions it provides and use them directly.

Php user-defined functions-php has a lot of built-in practical functions, but it cannot meet the actual design needs. at this time, we need to create our own functions.

Basic structure:

The code is as follows:

Function name (parameter ){
Statement body
}

Function is a declared function;

The function name must be unique. different from the variable name, the function name is case insensitive;
There must be spaces between function and function name;
You can omit or add multiple parameters separated by commas.
Php calls a function-when we define a function, we can call the function in the following structure:

Function name (parameter)

The parameter can be omitted, but parentheses must be kept.
Instance:

The code is as follows:

Function e (){
$ I = 5 + 6;
Echo $ I;
}
E ();
?>

Function transfer parameters

If a function defines a parameter, you must pay attention to the passing of the parameter value when calling the function. There are two ways to pass php function parameters: pass by value and pass by reference. the default value is pass by value.

1. pass by value:

Instance 1:

The code is as follows:

Function sum ($ num, $ price ){
$ Total = $ num * $ price;
Echo "total price: $ total ";
}
Sum (10, 78 );
?>

Default parameters

Default php function parameter-if the value of this parameter is not provided when a function is used, this parameter uses the default value set by the function. the default value must have been assigned when the function is defined.

Instance:

The code is as follows:

Function add ($ a, $ B = 10) {/* defines the variable $ B default parameter */
$ Sum = $ a + $ B;
Echo $ sum ."
";
}
Add ();/* pass parameters by value instead of default parameters */
Add (1);/* Default value */
?>

Function return value

When designing a program, we often do not want to display the function running result in the browser. in this case, we can use the return statement to call the function value before other operations.

Php function return value instance:

The code is as follows:

Function add ($ a, $ B = 10 ){
$ Sum = $ a + $ B;
Return $ sum;/* assign the value of $ sum to the function */
}
$ Sum = add ();/* the variable $ sum here gets the function value */
Echo $ sum ."
";
?>

Nested function call

The internal function can be either a general execution statement or a function, which is the use of nested php functions.

Function nesting only calls other functions in a function. here, we need to understand that all functions are in parallel, and the function definition is independent. Just like a container can hold many small containers, which are independent of each other and can hold their own things.

Instance:

The code is as follows:

Function volume ($ l, $ w ){
Function height ($ ){
$ A = $ a/2;
Return $;
}
$ A = $ l * $ w;
$ V = $ a * height ($ );
Echo $ v;
}
Volume (8, 3 );
?>

Recursive function call

What is a php recursive function? Before explaining this concept, let's take a look at a mathematical example. If you want to calculate the factorial of n, that is, n !, First, you should know (n-1 )!, But you need to know (n-1 )!, And must know (n-2 )!. And so on until 1! = 1, then 2 is returned !, 3 !... (N-1 )!, N !, In order to finally obtain the n factorial result.

The recursive call of a php function means that a function can call itself. This type of direct or brief call of the function itself is allowed in the php language and is a recursive function.

Instance:

The code is as follows:

Function calculate ($ n ){
If ($ n = 0)
Return 1;
Else
Return $ n * calculate ($ n-1 );
}
$ S = calculate (3 );
Echo $ s;
?>

...

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.