Home > Backend Development > PHP Tutorial > How to call a function in PHP

How to call a function in PHP

PHPz
Release: 2023-05-18 16:54:01
Original
2926 people have browsed it

In PHP programming, functions are one of the very important components. A function is a block of code that can be called multiple times to perform a specific task. In addition to improving code reusability and maintainability, using functions can also simplify code and improve development efficiency. This article will introduce how to call functions in PHP.

  1. Built-in functions

The PHP language has many commonly used functions built-in, such as string functions, time functions, file system functions, etc. To call a built-in function, just use the function name. Here are some examples of built-in functions:

// 字符串函数
$text = "hello world";
echo strlen($text); // 输出 11
echo strtoupper($text); // 输出 HELLO WORLD

// 时间函数
echo date("Y-m-d H:i:s"); // 输出当前时间

// 文件系统函数
$file = fopen("test.txt", "r");
echo fread($file, filesize("test.txt"));
fclose($file);
Copy after login
  1. Custom functions

Custom functions are functions written according to the specific needs of developers. It can be called anywhere in the program, improving code reusability and reducing code redundancy. To define a custom function in PHP, you need to use the function keyword. The following is a simple example of a custom function:

function add($a, $b) {
  return $a + $b;
}
echo add(1, 2); // 输出 3
Copy after login
  1. Anonymous function

Anonymous function is a function without a function name, also known as a closure function. Usually anonymous functions are used in scenarios where one-time functions need to be defined, such as callback functions and event handlers. To define an anonymous function in PHP, you need to use the function keyword and closing brackets (), and the function body needs to be wrapped in curly braces. The following is an example of an anonymous function:

$greet = function($name) {
  echo "Hello, {$name}!";
};
$greet("World"); // 输出 Hello, World!
Copy after login
  1. Function call

To call a function in PHP, you need to use the function name and a pair of parentheses() , actual parameters can be passed to the function. The following is an example of calling a function:

// 调用内置函数
echo strlen("hello"); // 输出 5

// 调用自定义函数
function add($a, $b) {
  return $a + $b;
}
echo add(1, 2); // 输出 3

// 调用匿名函数
$greet = function($name) {
  echo "Hello, {$name}!";
};
$greet("PHP"); // 输出 Hello, PHP!
Copy after login

When calling a function, you can pass parameters, separated by commas. PHP also supports default parameter values ​​and variable number of parameters. Here are some examples of different parameter types:

// 默认参数值
function greet($name = "World") {
  echo "Hello, {$name}!";
}
greet(); // 输出 Hello, World!
greet("PHP"); // 输出 Hello, PHP!

// 可变数量的参数
function add(...$numbers) {
  $sum = 0;
  foreach ($numbers as $number) {
    $sum += $number;
  }
  return $sum;
}
echo add(1, 2, 3, 4); // 输出 10
Copy after login
  1. Function return value

A function can return a value or an array, using return# at the end of the function ## keyword can return the value. The following is an example of a function return value:

// 返回一个值
function add($a, $b) {
  return $a + $b;
}
echo add(1, 2); // 输出 3

// 返回一个数组
function divide($a, $b) {
  return ["quotient" => $a / $b, "remainder" => $a % $b];
}
$result = divide(7, 3);
echo $result["quotient"];     // 输出 2
echo $result["remainder"];   // 输出 1
Copy after login
Summary

This article introduces several ways to call functions in PHP, including built-in functions, custom functions, anonymous functions, and function calls , parameter passing and return value, etc. Mastering the use of functions can improve the reusability and maintainability of code, improve development efficiency, and enhance code quality in PHP programming.

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