Home > Backend Development > PHP Tutorial > Detailed explanation of function declaration and usage in PHP

Detailed explanation of function declaration and usage in PHP

PHPz
Release: 2023-04-11 12:40:02
Original
1994 people have browsed it

PHP is a powerful, open source server-side scripting language widely used for web development. Among them, function is one of the most important components in PHP. A function is a reusable block of code that performs a specific task. This article will introduce how to use functions in PHP.

  1. Declaring a function

Declaring a function means defining the function name, parameters and function body so that PHP knows how to execute the function. The syntax is as follows:

function functionName($parameter1, $parameter2, ...){
  //函数体
  return $returnValue;
}
Copy after login

Among them,

  • functionName is the function name;
  • $parameter1, $parameter2 are the parameters of the function;
  • function body It is the specific task performed by the function; the
  • return keyword is used to specify the return value of the function.

For example, the following code defines a function called "getSum" that adds two numbers and returns their sum.

function getSum($num1, $num2){
  $sum = $num1 + $num2;
  return $sum;
}
Copy after login
  1. Calling a function

Calling a function means using the function name and corresponding parameters in the code to let PHP execute the function. Calling a function is very simple, just use the function name and parameter list. For example:

$sum = getSum(2,3);
echo $sum;
Copy after login

This code will output "5".

When calling a function, you must pass the correct number of parameters, otherwise an error will occur. If the function has no parameters, no parameters need to be passed when called.

  1. Function parameters

Function can have one or more parameters, separated by commas. When calling a function, actual values ​​are passed to the parameters. For example:

function getFullName($firstName, $lastName){
  $fullName = $firstName . ' ' . $lastName;
  return $fullName;
}
Copy after login

Call this function:

$name = getFullName('Tom', 'Smith');
echo $name;
Copy after login

will output "Tom Smith".

There is also a parameter type called default parameters, which have default values. When calling a function, if no parameters are passed, the default values ​​will be used. For example:

function printNumber($num = 0){
  echo $num;
}
Copy after login

Call this function:

printNumber(); // 输出“0”
printNumber(10); // 输出“10”
Copy after login
  1. Variable scope

Variables defined within the function can only be defined within the function For internal use, they are called local variables. Variables defined outside a function are called global variables and can be used both inside and outside the function.

The advantage of global variables is to share data between functions, but improper use can cause program errors. Therefore, when writing functions, you should try to avoid using global variables.

  1. Function return value

Function can return a value for use when calling the function. For example:

function getAverage($num1, $num2, $num3){
  $sum = $num1 + $num2 + $num3;
  $average = $sum / 3;
  return $average;
}
Copy after login

Call this function:

$result = getAverage(80, 90, 70);
echo $result;
Copy after login

will output "80".

  1. Anonymous functions

PHP supports anonymous functions, also known as closure functions. They have no names and can be saved, passed and executed in variables. For example:

$greeting = function($name){
  echo 'Hello, ' . $name;
};

$greeting('Tom'); // 输出“Hello, Tom”
Copy after login
  1. Callback function

A callback function is a function passed in the function parameters to be called when another function is executed . They are very useful for event handling, sorting algorithms, etc. For example:

function processArray($array, $callback){
  foreach($array as $value){
    $callback($value);
  }
}

function printValue($value){
  echo $value . ' ';
}

$array = [1, 2, 3, 4, 5];
processArray($array, 'printValue'); // 输出“1 2 3 4 5”
Copy after login

The above is the usage of PHP function. Functions allow us to reuse code, save time, and improve code readability. When you need some specific functionality, just define a function and call it where needed.

The above is the detailed content of Detailed explanation of function declaration and usage in PHP. 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