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. 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. below is the syntax structure of PHP declarative functions:

 

The Code is as follows: Copy code

Function function_name ($ argument1, $ argument2, $ argument3,... $ argumentn)

{

// Function code

Return value;

}

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

(1) function: Used to declare the keywords of user-defined functions.

(2) function_name: name of the function to be created. this name will be used for 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. But 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, no braces are required.

(5) Return: Return the value required by the called code. any type can be returned, including list and object. 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 Code is as follows: Copy code

<? Php
Require 'a. php ';
Echo "I won't be executed! ";

?>

3. Parameter Functions

PHP supports passing parameters by value (default), passing parameters by 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 Code is as follows: Copy code
<? Php
Function sum ($ a, $ B)
{
Echo $ a + $ B;
}
Sum (); // start to call this function
?>

(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 Code is as follows: Copy code

<? Php
$ MyNum = 100;
Function Valuechange ($ number)
{
$ Number = $ number + 1;
Echo $ number. "<br> ";
}
Valuechange ($ myNum );
Echo $ myNum;
?>

 

(3) default value parameter. when using the default parameter value transfer method, the function must have a parameter during the call. 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 Code is as follows: Copy code
<? Php
$ MyNum = 100;
Function Valuechange ($ number)
{
$ Number = $ number + 1;
Echo $ number. "<br> ";
}
Valuechange ($ myNum );
Echo $ myNum;
?>

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.