Home > Backend Development > PHP Tutorial > How to call php custom function

How to call php custom function

伊谢尔伦
Release: 2023-03-07 17:58:01
Original
6607 people have browsed it

Whether it is a custom function or a system function, if these functions are not called, it will not be executed. In general, you just need to use the function name and parameter list to call the function where you need to use it. After the function is called, it starts executing the code in the function body. After the execution is completed, it returns to the calling location and continues execution below. Therefore, the name of the function when calling the function can be summarized as the following three functions.


(1) Call the function through the function name, let the code of the function body run, and call the function several times body, the code will be executed several times.

(2) If the function has a parameter list, you can also pass the corresponding value to the parameter through the parentheses after the function name, and use the parameters in the function body to change the internal code of the function. Execution behavior.

(3) If the function has a return value, after the function is executed, the value after return will be returned to the location where the function was called, so that the function name can be regarded as the function's Use the returned value.


## As long as the declared function is visible in the script, it can be called anywhere in the script through the function name. In PHP, you can call a function after it is declared, you can call it before the function is declared, and you can also call a function within a function. Sometimes, although a function is declared, if it is not called, the function will not be executed. If we call it once before and once after the function declaration, the function will be executed twice.


## Function writes some repeated functions in an independent code block and calls them separately when needed. Previously in

How to declare a php custom function we introduced how to create a function custom function. After the function is defined, all that needs to be done is to call the function. The operation is It's relatively simple, just quote the function name and assign the correct parameters.

Calling php custom function instance

The following defines a function test() to calculate the square of the incoming parameters, and then together with the expression All formulas and results are output, and the code is displayed as follows:

<?php
  function test($num)
  {
     return "$num * $num = ". $num * $num;
  }
  echo test(10);
?>
Copy after login

The result is:

10 * 10 = 100


Explanation: $num in the test() function above is a parameter in the custom function. We will introduce it in detail in the next chapter

Two parameters of php custom function.

The above is the detailed content of How to call php custom function. 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