In PHP, more than 700 built-in functions are provided.
PHP functions are divided into user-defined functions and system built-in functions. Built-in functions can be used directly, and user-defined functions need to be defined using the keyword function.
Custom function
A function can be seen as a collection of independent program statements that implement a certain function. After we write a certain function into a function, we can use it conveniently where needed. Reasonable use of functions can make our PHP programs more concise, easy to read, and more scientific.
Grammar
The code is as follows
代码如下 |
复制代码 |
function function_name(arg1,arg2,……)
{
函数功能代码
}
|
|
Copy code
|
function function_name(arg1,arg2,……)
{
Function code
}
Create PHP function
A function is a block of code that can be executed whenever needed.
Create PHP function:
All functions start with the keyword "function()"
Naming functions - The name of a function should hint at its functionality. Function names start with a letter or underscore.
代码如下 |
复制代码 |
function writeMyName()
{
echo "David Yang";
}
echo "Hello world! ";
echo "My name is ";
writeMyName();
echo ". That's right, ";
writeMyName();
echo " is my name.";
?>
以上代码的输出:
Hello world!
My name is David Yang.
That's right, David Yang is my name.
|
Add "{" - the part after the opening curly brace is the function's code.
Insert function code
Add a "}" - the function ends by closing the curly brace.
Function parameters
代码如下 |
复制代码 |
function city_name($city)
{
echo "城市名称为:".$city;
}
city_name("shanghai"); //执行该函数,执行结果是输出“城市名称为:shanghai”字符串
?>
可以给函数的参数指定默认值,以便在没有指定参数值时,采用参数默认值。
function city_name($city = "beijing")
{
echo "城市名称为:".$city;
}
$name = "shanghai";
city_name(); //执行结果是输出“城市名称为:beijing”
city_name($name); //执行结果是输出“城市名称为:shanghai”
?>
|
The function of parameters is to pass information to the function. |
Example
Now, we are 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, ";
echo " is my name.";
?>
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 "The city name is: ".$city;<🎜>
}<🎜>
city_name("shanghai"); //Execute this function, the execution result is to output the string "The name of the city is: shanghai"<🎜>
?>
You can specify default values for function parameters so that when no parameter value is specified, the parameter default value is used.
function city_name($city = "beijing")<🎜>
{<🎜>
echo "The city name is: ".$city;<🎜>
}<🎜>
$name = "shanghai";<🎜>
city_name(); //The execution result is the output "The city name is: beijing"<🎜>
city_name($name); //The execution result is to output "The city name is: shanghai"<🎜>
?>
http://www.bkjia.com/PHPjc/629078.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629078.htmlTechArticleIn PHP, more than 700 built-in functions are provided. PHP functions are divided into user-defined functions and system built-in functions. Built-in functions can be used directly, user-defined functions need to use...
|
|
|