PHP Custom Function declaration, invocation, parameters and return value Basics Tutorial

Source: Internet
Author: User
Like a function in math, y=f(x)is the basic form of expression of a function, xCan be seen as a parameter, yCan be seen as a return value, that is, a function definition is a named, separate code snippet that performs a specific task and may return a value to the program that called it.

Custom functions

Declaration of a function

In the process of writing programs, we often encounter some functional modules in PHP does not provide system functions, so we need to define our own functions, that is, custom functions, the rules of custom functions are as follows:

    • The first line of each function is the function header, which consists of the keyword function , function name, and argument list that declare the functions.

    • Each custom function must use the function keyword declaration.

    • The function name can represent the entire function, and you can name the function any name, as long as you follow the naming conventions of the variable names. However, the function name must be unique and cannot be the same as the system function.

    • The curly braces that follow the function name are also required when declaring a function, indicating a set of acceptable parameter lists in curly brackets, which are declared variables and then passed to the value when the function is called. The argument list can have no or one or more parameters, and multiple parameters are separated by commas.

    • The function body is located behind the function head, enclosed in curly braces. After the function is called, it executes to the retun end of the statement or the outermost curly brace, returning to the calling program.

    • Use the keyword to return return a value from the function and return to the calling program to continue execution.

Demo

<?php     /* The code that uses the double-layer for loop output table is declared as a function, and the function name is table *    /Functions table () {                                      echo "<table align= ' center ' border= ' 1 ' width= ' > ';                   for ($out =0; $out <, $out + +) {                   $bgcolor = $out%2 = = 0? "Red": "Blue";//Line change background color                        echo "<tr bgcolor=". $bgcolor. " > ";             for ($in =0; $in <10; $in + +) {                    echo "<td>". ( $out *10+ $in). " </td> ";                }            echo "</tr>";            }        echo "</table>";    } ?>

Call to function

Whether it is a custom function or a system function, if the function is not called, it will not execute. After the function is called, the code in the body of the function is executed, and execution is returned to the place where the call continues. The calling rules are as follows:

    • The function is called by the function name.

    • If the function has a parameter list, you can also pass in the corresponding value to the parameter through parentheses after the function name, and use parameters in the body of the function to change the execution behavior of the function's internal code.

    • If the function has a return value, the subsequent value is returned to the location where the function is called when the function finishes executing return .

Demo

<?php     /* The code that uses the double-layer for loop output table is declared as a function, and the function name is table *    /Functions table () {                                      echo "<table align= ' center ' border= ' 1 ' width= ' > ';                   for ($out =0; $out <, $out + +) {                   $bgcolor = $out%2 = = 0? "Red": "Blue";//Line change background color                        echo "<tr bgcolor=". $bgcolor. " > ";             for ($in =0; $in <10; $in + +) {                    echo "<td>". ( $out *10+ $in). " </td> ";                }            echo "</tr>";            }        echo "</table>";    } ?>  <?php table ();? >

Parameters of the function

A parameter list is made up of 0, one, or more parameters. Each parameter is an expression, separated by commas. For an argument function, there is a data transfer relationship between the PHP script and the called function. When defining a function, the expression in parentheses after the function name is called the formal parameter ("formal parameter"), and the expression in parentheses after the called function name is called the actual parameter ("argument"), and the argument and the formal parameter need to pass the data in the order corresponding. If the function does not have a parameter list, the function is fixed, and the user cannot change some of the execution behavior inside the function when calling the function.

Demo

<?php     /** a custom Function table (), declare three parameters, separating the parameters with commas            @param  string  $tableName  requires a table name of a string type            @ param  int     $rows       need an integer number to set the number of rows in the table            @param  int     $cols       need another integer value to set the column count of the table    *    / function table ($tableName, $rows, $cols) {                            echo "<table align= ' center ' border= ' 1 ' width= ' >";               echo "<caption>

return value of the function

The return value of the function is the result of the function execution, and the script that invokes the function cannot directly use the information inside the function body, but can pass the data to the caller through the keyword return . The return statement notes are as follows:

    • returnStatement to return the function caller to any execution result value in the body of the function.

    • If a statement is executed in the body of the function return , the statement after it is not executed.

Demo

<?php/** a custom Function table (), declare three parameters, separating the parameters with commas @param string $tableName requires a table name of type string @pa The RAM int $rows requires an integer number to set the number of rows in the table @param int $cols need a different integer value to set the table's column count */function table ($        TableName, $rows, $cols) {$returnStr = "This is the returned string";               echo "<table align= ' center ' border= ' 1 ' width= ' >";                        echo "<caption>

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.