Home >Backend Development >PHP Tutorial >PHP function writing guide: basic syntax and calling methods
PHP is a popular scripting language widely used in web development. It formulates well-designed function syntax and calling methods, which can improve development efficiency and code readability. For beginners, writing PHP functions can be a bit difficult. Therefore, this article will start from two aspects: basic syntax and calling methods to help readers better understand and master PHP function writing.
I. Basic syntax
PHP function consists of function
identifier, function name, parameter list and function body. The following is a simple example:
function greeting($name) { echo "Hello, " . $name . "!"; }
where greeting
is the function name, $name
is the variable name in the parameter list, and in the function body The echo
statement outputs a string. When the function is called, the actual parameters are passed to the formal parameters $name
. For example:
greeting("John");
will output the following results:
Hello, John!
The statements in the function body can have return values, which are defined using the return
keyword. For example:
function add($a, $b) { return $a + $b; }
Calling this function:
$result = add(3, 5); echo $result;
will output:
8
Note that function names are not case-sensitive. Therefore, greeting
and Greeting
are equivalent. However, for the sake of code readability, it is recommended to follow a unified naming convention.
II. Calling method
There are two ways to call PHP functions: through the general calling methods call_user_func()
and call_user_func_array()
, and directly Call name.
call_user_func()
and call_user_func_array()
can call any callable function and will variables are passed to them. For example:
function testing($str) { echo "This is a testing function with parameter: " . $str; } call_user_func('testing', 'hello world');
will output:
This is a testing function with parameter: hello world
Of course, you can also save the function name in a variable:
$func_name = 'testing'; call_user_func($func_name, 'hello world');
call_user_func_array()
is used the same way call_user_func()
Similar, except that its second parameter is an array containing the parameters to be passed to the function. For example:
function sum($a, $b, $c) { return $a + $b + $c; } $args = [1, 2, 3]; $result = call_user_func_array('sum', $args); echo $result;
will output:
6
Direct call name refers to calling directly using the function name and parameter list. For example:
function multiply($a, $b) { return $a * $b; } echo multiply(2, 3);
will output:
6
This method is the most intuitive and commonly used. However, it can only directly call functions defined in the current script and cannot be imported from other files.
Summary
This article briefly introduces the basic syntax and two calling methods of PHP functions. Writing and calling functions is an inevitable part of PHP development. We need to adhere to function specifications to ensure code readability and maintainability. Mastering PHP function writing skills can not only improve programming skills, but also improve efficiency in daily work.
The above is the detailed content of PHP function writing guide: basic syntax and calling methods. For more information, please follow other related articles on the PHP Chinese website!