PHP function syntax includes function declaration (function_name and parameter list) and function body (containing the code to be executed). Functions can define a return value type, and if it is not defined, NULL is returned. Examples include calculating the sum of two numbers and checking whether a string contains another string.
The syntax of PHP function is as follows:
function [修饰符] function_name([参数列表]) { // 函数体 }
Where:
[Modifier]
is optional and can be public
, private
, protected
or static
. function_name
is the name of the function. [Parameter List]
is optional and contains the list of parameters accepted by the function. The function body
is the block of code that contains the code to be executed by the function. PHP function consists of the following parts:
function
, [modifier]
and function_name
) and parameter list. NULL
. Example 1:Calculate the sum of two numbers
function add($num1, $num2) { return $num1 + $num2; } $result = add(10, 15); echo $result; // 输出:25
Example 2:Judge one Whether a string contains another string
function contains($str, $subStr) { return strpos($str, $subStr) !== false; } $contains_result = contains("Hello world", "world"); echo $contains_result ? '包含' : '不包含'; // 输出:包含
The above is the detailed content of Syntax and structure of PHP functions. For more information, please follow other related articles on the PHP Chinese website!