Home > Backend Development > PHP Tutorial > How to declare and apply various forms of php functions

How to declare and apply various forms of php functions

伊谢尔伦
Release: 2023-03-07 18:30:01
Original
1680 people have browsed it

When writing a PHP program, you can define functions yourself. Of course, it would be best if there are directly available functions in the PHP system. If not, you will define them yourself. There are many standard functions available for use in the PHP system, but some functions require specific PHP extension modules to be compiled together, otherwise you will get a fatal " Undefined function" when using them. mistake. For example, to use the image function imagecreatetruecolor(), you need to add GD support when compiling PHP; or to use the mysql_connect() function, you need to add MYSQL support when compiling PHP. There are many core functions that have been included in every version of PHP, such as string and variable functions. Call the phpinfo() or get_loaded_extensions() function to know which extension libraries are loaded by PHP. It should also be noted that many extension libraries are available by default.

The method of calling system functions is the same as calling custom functions. Every function provided by the system will have detailed help information, so there is no need to spend a lot of time studying how the function is executed internally when using the function. As long as we refer to the help document to complete the function call, we can achieve the functions we need. That's it. Of course, if you declare a function for others to use, you should also provide a detailed usage instructions for the function. If you want to successfully apply a function through the help document, the help document introducing the use of the function needs tocontain the following points:

1. Function description of the function (deciding whether to use this function ).

Which function to use to complete what task needs to be determined, so the function description of the function allows us to decide whether to use it in our own scripts.

2. Parameter description (deciding how to use this function)

The function of parameters is to import certain values ​​before executing the function, and provide function processing and execution. Passing values ​​through function parameters can change the internal execution behavior of the function, so the detailed description of how to pass values, what values ​​to pass, what type of values ​​to pass, and how many values ​​to pass are the key to deciding how to use the function.

3. Return value (how to handle it after the call)

In the script, the next step of execution of the program is determined by obtaining the return value after the function call, so it is necessary to know whether the function returns value, what value is returned, and what type of value is returned.

The following is an example of a custom function, including the above three aspects of help information:

<?php
  //定义一个计算两个整数乘积的函数
  function index($a, $b){  //两个整数参数
    $sum = 0;              //声明一个变量保存计算后的结果
    $sum = $a * $b;
    return $sum;           //返回值
  }
  echo index(6,8);         //应用函数
?>
Copy after login

PHPThe parameters of the function are what determine how Criteria for successfully applying a function or controlling the behavior of a function execution. And because PHP is a weakly typed language, there are many ways to set up and apply parameters. Therefore, learning to declare functions with different parameters and to successfully call functions with various forms of parameters is the key to learning functions. Later, the corresponding declarations and applications will be introduced through the parameter characteristics of PHP functions.

【Recommended related tutorials】

1. "php.cn Dugu Jiujian (4)-php video tutorial"

2. php programming from entry to master a full set of video tutorials

3. php practical video tutorial

The above is the detailed content of How to declare and apply various forms of php functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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