Learning notes for php functions

Source: Internet
Author: User
In php, functions are commonly used as a reasonable method that can be used repeatedly. functions are system functions and user-defined functions, respectively, the following describes how to use these two 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, the following describes how to use 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, as long as we know the functions provided by it can be used directly.

Php user-defined functions-although php has many built-in practical functions, it cannot meet the actual design requirements. at this time, we need to create functions by ourselves.

Basic structure:

The instance code is as follows:

  1. Function name (parameter ){
  2. Statement body
  3. }

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 the function name and the function name. The parameter can be omitted, you can also 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.

The instance code is as follows:

  1. Function e (){
  2. $ I = 5 + 6;
  3. Echo $ I;
  4. }
  5. E ();
  6. ?>

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:

The instance code is as follows:

  1. Function sum ($ num, $ price ){
  2. $ Total = $ num * $ price;
  3. Echo "total price: $ total ";
  4. }
  5. Sum (10, 78 );
  6. ?>

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.

The instance code is as follows:

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

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 call the function value through the return statement before performing other operations.

Php function return value instance:

The instance code is as follows:

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

Nested function call

The function can not only be a general execution statement, but also a function. this 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 a parallel relationship and function definitions are independent. just like a container can hold many small containers, which are independent of each other and can hold their own things.

The instance code is as follows:

  1. Function volume ($ l, $ w ){
  2. Function height ($ ){
  3. $ A = $ a/2;
  4. Return $;
  5. }
  6. $ A = $ l * $ w;
  7. $ V = $ a * height ($ );
  8. Echo $ v;
  9. }
  10. Volume (8, 3 );
  11. ?>

Recursive function call

What is a php recursive function? Before explaining this concept, let's look at a mathematical example. if we 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.

The instance code is as follows:

  1. Function calculate ($ n ){
  2. If ($ n = 0)
  3. Return 1;
  4. Else
  5. Return $ n * calculate ($ n-1 );
  6. }
  7. $ S = calculate (3 );
  8. Echo $ s;
  9. ?>

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.