Function usage in php

Source: Internet
Author: User
In the languages I know, the function command is used to define functions. next we will introduce the usage of functions in php. User-defined functions are also called Udfs. they are not provided by PHP and are created by programmers.

In the languages I know, the function command is used to define functions. next we will introduce the usage of functions in php.

User-defined functions are also called Udfs. they are not provided by PHP and are created by programmers. Because you have created such functions, you can fully control these functions. Therefore, a function can be run in the desired way.

1. declare a function

In PHP, the methods for defining functions are almost the same as those in other programming languages. The syntax structure of the PHP declarative function is as follows:

The instance code is as follows:

  1. Function function_name ($ argument1, $ argument2, $ argument3 ,...... $ Argumentn)
  2. {
  3. // Function code
  4. Return value;
  5. }

In the above syntax structure, the meaning of the keyword is as follows.

(1) function: keywords used to declare user-defined functions.

(2) function_name: name of the function to be created. This name will be used in future calls. The function name must be unique because PHP does not support overloading. When naming a function, you must follow the variable name.

The same principle. However, the function name cannot start with $, and the variable can.

(3) argument: the value to be passed to the function. A function can have multiple parameters with commas. However, parameter items are optional. you can call a function without passing any parameters.

(4) code: A piece of code executed when the function is called. If there are two or more statements, the code must be enclosed in braces. However, if there is only one code, braces are not required.

(5) Return: Return the value required by the called code. All types can be returned, including lists and objects. This causes the function to immediately end its operation and pass control back to the called row.

2. no parameter functions

Code func_1. Php

The instance code is as follows:

  1. Require 'a. Php ';
  2. Echo "I won't be executed! ";
  3. ?>

3. parameter functions

PHP supports passing parameters by value (default) through reference and default parameter values. The variable length parameter list is only supported in PHP4 and later versions.

(1) value transfer parameters. Passing parameters by value is the default transfer method of PHP. To use this method, you must pass a value (parameter) when calling the main program ).

Code sum. Php

The instance code is as follows:

  1. Function sum ($ a, $ B)
  2. {
  3. Echo $ a + $ B;
  4. }
  5. Sum (); // start to call this function
  6. ?>

(2) reference parameters. When passing by value, only a copy of the parameter is passed to the called function. However, any modification to these values within the called function does not affect the call of the function.

The original value in. The reference transfer is actually the address transfer, and the address of a variable is passed as a parameter.

Code valuechange. Php

The instance code is as follows:

  1. $ MyNum = 100;
  2. Function Valuechange ($ number)
  3. {
  4. $ Number = $ number + 1;
  5. Echo $ number. "
    ";
  6. }
  7. Valuechange ($ myNum );
  8. Echo $ myNum;
  9. ?>

(3) default value parameter. When using the default parameter value transfer method, the function must have a parameter when calling. If no value is used, the default value is passed to the function parameter. The default value must be a constant expression, not a variable,

Class member or function call.

Tip: When using default parameters, any default parameters must be placed on the right of non-default parameters; otherwise, the function may not work as expected.

Code func_default. Php

 

The instance code is as follows:

  1. $ MyNum = 100;
  2. Function Valuechange ($ number)
  3. {
  4. $ Number = $ number + 1;
  5. Echo $ number. "
    ";
  6. }
  7. Valuechange ($ myNum );
  8. Echo $ myNum;
  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.