Home > Backend Development > PHP Tutorial > Parameter passing guide in PHP function calls

Parameter passing guide in PHP function calls

PHPz
Release: 2024-04-16 11:39:02
Original
720 people have browsed it

In PHP function calls, parameter passing includes passing by value, passing by reference and default parameters. Passing by value copies the variable value, passing by reference modifies the original variable, and default parameters provide predefined values. Practical examples demonstrate pass-by-value calculation of taxes, pass-by-reference exchange variables, and post creation with default parameters.

PHP 函数调用中的参数传递指南

Guidelines for passing parameters in PHP function calls

When writing PHP functions, it is crucial to understand how to pass parameters. This article will delve into the parameter passing mechanism in PHP function calls and provide practical examples to help you master this key concept.

How to pass parameters

In PHP, parameters can be passed in many ways:

  • Passed by value: The passed variable value is copied instead of modifying the original variable.
  • Pass by reference: A reference to the passed variable is passed, allowing the function to modify the original variable.
  • Default parameters: Specify predefined values ​​for the function. If the actual parameters are omitted when calling, the default values ​​are used.

Pass by value

The following code demonstrates pass by value:

function sum($a, $b) {
    $a += $b;
}

$x = 10;
$y = 20;

sum($x, $y);

echo $x; // 输出:10
Copy after login

In this example, x and y variables are passed by value to the sum() function. Operations within the function do not modify the original variable, so echo $x outputs 10, not 30.

Pass by reference

Pass by reference allows a function to modify the original variable. To pass variables by reference, use the & symbol before the function parameter:

function sumByReference(&$a, &$b) {
    $a += $b;
}

$x = 10;
$y = 20;

sumByReference($x, $y);

echo $x; // 输出:30
Copy after login

In this example, the x and y variables are by reference Passed to the sumByReference() function. The operations within the function modify the original variable, so echo $x outputs 30.

Default parameters

Default parameters allow you to specify predefined values ​​for function parameters. The following code demonstrates how to use default parameters:

function greet($name = "World") {
    echo "Hello, $name!";
}

greet(); // 输出:Hello, World!
greet("John"); // 输出:Hello, John!
Copy after login

In this example, the name parameter is assigned a default value of "World". If no parameter value is explicitly provided at call time, the default value will be used.

Practical case

Using pass by value

function calculateTax($amount, $rate) {
    return $amount * $rate;
}

$amount = 100;
$rate = 0.08;

$tax = calculateTax($amount, $rate);

echo $tax; // 输出:8
Copy after login

In this case, amount and rate The variable is passed by value to the calculateTax() function, which returns the amount of tax payable.

Using pass by reference

function swap(&$a, &$b) {
    $temp = $a;
    $a = $b;
    $b = $temp;
}

$a = 10;
$b = 20;

swap($a, $b);

echo $a; // 输出:20
echo $b; // 输出:10
Copy after login

In this case, the a and b variables are passed by reference to the swap() function , the function exchanges the values ​​of variables.

Use default parameters

function createPost($title, $content = "") {
    // 创建新的帖子...
}

createPost("My Post"); // 使用默认的内容值 ""
createPost("My Post", "This is the content."); // 覆盖默认值
Copy after login

In this case, the second content parameter has a default value of "". This parameter can be omitted when calling the function, or a custom value can be provided to override the default value.

The above is the detailed content of Parameter passing guide in PHP function calls. 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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template