PHP Function


PHP function is divided into user-defined functions and system built-in function. Built-in functions can be used directly, user-defined functions need to use the keyword function to define.


Custom functions

A function can be seen as a collection of independent program statements that implement a function. We will write a function as a function, you can easily use it where needed. Reasonable use of functions, we can make our PHP program more simple and easy to read, more scientific.

grammar

function function_name(arg1, arg2, ...)
{
    // function body
}

Grammatical Interpretation:

  1. Define a function with the function keyword
  2. The function name is followed by a function name, which begins with a letter or an underscore and should be named for its function
  3. The function name is followed by a pair of parentheses, which are arguments to the function, separated by a "," but not necessarily
  4. The brackets() are followed by "{}", and the inside is the function statement to be implemented by the function

The following is an example of a custom function:

/* Define function start */
function print_string()
{
    Hello " Hello! ";
}
/* Define function end */
print_string(); // The implementation of the function, the implementation of the output is "Hello!" string

Any valid PHP code may appear inside a function, including even other functions and class definitions.


Tip

function names are case-insensitive, but when calling functions, they are usually used in the same form as they were defined.


The argument to the function

The function of a parameter is to pass information to a function.

Example of using parameters:

function city_name($city)
{
    echo " city name is: ". $city;
}
// Execute the function, the result is output "city name: shanghai" string

You can specify a default value for the function's parameters so that parameter defaults are used when no parameter value is specified.

function city_name($city =" london ")
{
    echo " city name is: ". $city;
}
$name =" shanghai ";
city_name(); // output "City name is: london"
city_name($name); // The implementation of the output is "city name is: shanghai"

As you can see from the example, the variable name($name) for the passed-in argument is independent of the parameter variable name($city) for the function.

Accept multiple parameters:

function city_name($city, $zipcode)
{
    echo " city name is: ". $city. &Quot; <br /> ";
    echo " zipcode: ". $zipcode;
}

The return value of the function

function in dealing with the internal logic, we often need to decide the next action based on the results of the operation logic, this time need to get the results of the function. Use return() to return the result of the function.

Examples of function return values:

function add($x)
{
    return $x + 1;
}
echo add(2); // output function of the return value, the result is output 3

function return value does not mean to return a value, you can return to include strings, arrays, objects, including any type.

Compare the differences between the following two examples:

$x = 10;
function multiply($x) {
    $x = $x * 10;
    return $x;
}
multiply($x);
echo $x; // output 10
$x = 10;
function multiply($x) {
    $x = $x * 10;
    return $x;
}
$x = multiply($x);
echo $x; // output 100

built-in function

PHP built a lot of functions, which makes PHP become very powerful. These functions include almost all aspects of the need to deal with us.

PHP built-in functions commonly used are "string function", "array function", "database function", "time /date function", "file function", "image function", "regular function", "URL function".




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook