In PHP, more than 700 built-in functions are available. PHP functions are divided into user-defined functions and system-built functions. Built-in functions can be used directly, and user-defined functions need to be defined using the keyword function.
Custom functions
function, which can be seen as a separate set of program statements to implement a function. Once we've written a function into a function, we can use it conveniently wherever we need it. Reasonable use of functions, can make our PHP program more concise and easy to read, more scientific.
Grammar
The code is as follows |
Copy Code |
function function_name (arg1,arg2,......) { function function code } |
Creating PHP Functions
A function is a block of code that can be executed at any time when it is needed.
To create a PHP function:
All functions use the keyword "function ()" to start
Named function-the name of the function should indicate its function. The function name begins with a letter or underscore.
Add "{"-the section after the opening curly brace is the code of the function.
inserting function code
Add a "}"-the function ends by closing the curly braces.
Parameters of the function
The function of a parameter is to pass information to a function.
Example
Now, we're going to use this function in our PHP script:
The code is as follows |
Copy Code |
function Writemyname () { echo "David Yang"; } echo "Hello world! "; echo "My name is"; Writemyname (); echo ". That's right, "; Writemyname (); echo "is my name."; ?> The output of the above code:
Hello world! My name is David Yang. That's right, David Yang is my name. |
Example:
The code is as follows |
Copy Code |
function City_name ($city) { echo "City name is:". $city; } City_name ("Shanghai"); Executes the function, and the result is output "city Name: Shanghai" string ?> You can specify a default value for the parameters of the function so that the parameter defaults are taken when no parameter values are specified. function City_name ($city = "Beijing") { echo "City name is:". $city; } $name = "Shanghai"; City_name (); Execution result is output "city name: Beijing" City_name ($name); Execution result is output "city name: Shanghai" ?> |
http://www.bkjia.com/PHPjc/629078.html www.bkjia.com true http://www.bkjia.com/PHPjc/629078.html techarticle in PHP, more than 700 built-in functions are available. PHP functions are divided into user-defined functions and system-built functions. Built-in functions can be used directly, user-defined functions need to be used ...