How to declare php UDFs

Source: Internet
Author: User
In real projects, when php functions cannot meet our needs, we can write our own functions to complete any task. this is a custom function. PHP built-in functions can be used in user-defined functions. in real projects, when php functions cannot meet our needs, we can write our own functions to complete any task, this is a user-defined function. In the previous article, we explained in detail the concept of user-defined functions and introduced what are php user-defined functions? So how to declare php user-defined functions?The following describes the basic structure of php user-defined functions.

The following syntax format can be used to declare a custom function in php:

 

Syntax format explanation of the function:

(1) the first line of each function is the function header, which consists of three parts: the function that declares the function keyword, the function name, and the parameter list. each part of the function completes a specific function.

(2) each custom function must be declared with the "function" keyword.

(3) the function name can represent the entire function. you can name the function as any name. you only need to follow the naming rules of the variable name. Each function has a unique name. However, you must note that function overloading cannot be used in php, so you cannot define a function with a duplicate name, or a function with the same name as a system function. Specifying a function name to describe its function is a common practice during development.

(4) when declaring a function, the brackets following the function name are also required. The brackets indicate a list of acceptable parameters. the parameters are declared variables, then, some values are passed to the function when it is called. The parameter list can be left blank or not written. it can contain one or more parameters. multiple parameters must be separated by commas.

(5) place the function body behind the function header and enclose it in curly brackets. The actual work is done in the function body. After a function is called, first execute the first statement of the function weight, execute the return statement or the outermost curly braces, and then return to the called program. Any valid php code can be used in the function body, and other functions or classes can be declared in the function body.

(6) you can use the return keyword to return a value from the function. Add an expression after return. when the program executes the return statement, the expression is calculated and then returned to the calling program for further execution. The return value of the function is the value of the expression.

Php user-defined function declarative instance

Next we will use a simple example to introduce how to customize a function sun (), calculate the sum of the two input parameters, and then output all the results together with the expressions and results.

 

Result:5 + 8 = 13

The preceding section describes how to declare php Udfs. the next section describes how to call php Udfs.

The above is how to declare the details of php user-defined functions. For more information, see other related articles in the first PHP community!

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.