Home > Backend Development > PHP Tutorial > PHP function writing guide: basic syntax and calling methods

PHP function writing guide: basic syntax and calling methods

王林
Release: 2023-06-11 09:10:01
Original
1189 people have browsed it

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 functionidentifier, function name, parameter list and function body. The following is a simple example:

function greeting($name) {
    echo "Hello, " . $name . "!";
}
Copy after login

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");
Copy after login

will output the following results:

Hello, John!
Copy after login

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;
}
Copy after login

Calling this function:

$result = add(3, 5);
echo $result;
Copy after login

will output:

8
Copy after login

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.

  1. General calling method

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');
Copy after login

will output:

This is a testing function with parameter: hello world
Copy after login

Of course, you can also save the function name in a variable:

$func_name = 'testing';
call_user_func($func_name, 'hello world');
Copy after login

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;
Copy after login

will output:

6
Copy after login
Copy after login
  1. Direct call name

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);
Copy after login

will output:

6
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template