& 什么是变量前面的符号表示什么意思?

发布于 2024-10-02 12:02:38 字数 203 浏览 5 评论 0原文

我正在“剖析”PunBB,它的功能之一是检查 BBCode 标签的结构,并尽可能修复简单的错误:

function preparse_tags($text, &$errors, $is_signature = false)

$error& 是什么意思? > 变量是什么意思?

I'm 'dissecting' PunBB, and one of its functions checks the structure of BBCode tags and fix simple mistakes where possible:

function preparse_tags($text, &$errors, $is_signature = false)

What does the & in front of the $error variable mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨离汐 2024-10-09 12:02:38

这意味着通过引用传递变量,而不是传递多变的。这意味着当程序流返回到调用代码时,对 preparse_tags 函数中该参数的任何更改都会保留。

function passByReference(&$test) {
    $test = "Changed!";
}

function passByValue($test) {
    $test = "a change here will not affect the original variable";
}

$test = 'Unchanged';
echo $test . PHP_EOL;

passByValue($test);
echo $test . PHP_EOL;

passByReference($test);
echo $test . PHP_EOL;

输出:

不变

不变

改变了!

It means pass the variable by reference, rather than passing the value of the variable. This means any changes to that parameter in the preparse_tags function remain when the program flow returns to the calling code.

function passByReference(&$test) {
    $test = "Changed!";
}

function passByValue($test) {
    $test = "a change here will not affect the original variable";
}

$test = 'Unchanged';
echo $test . PHP_EOL;

passByValue($test);
echo $test . PHP_EOL;

passByReference($test);
echo $test . PHP_EOL;

Output:

Unchanged

Unchanged

Changed!

蓝眼泪 2024-10-09 12:02:38

它确实通过引用传递而不是通过值传递。

这允许函数在调用函数的范围内更改其自身范围之外的变量。

例如:

function addOne( &$val ) {
    $val++;
}
$a = 1;
addOne($a);
echo $a; // Will echo '2'.

preparse_tags 函数的情况下,它允许函数返回已解析的标签,但允许调用父级获得任何错误,而无需检查返回值的格式/类型。

It does pass by reference rather than pass by value.

This allows for the function to change variables outside of its own scope, in the scope of the calling function.

For instance:

function addOne( &$val ) {
    $val++;
}
$a = 1;
addOne($a);
echo $a; // Will echo '2'.

In the case of the preparse_tags function, it allows the function to return the parsed tags, but allow the calling parent to get any errors without having to check the format/type of the returned value.

北方的韩爷 2024-10-09 12:02:38

它接受对变量的引用作为参数。

这意味着函数对参数所做的任何更改(例如,$errors = "Error!")都会影响调用函数传递的变量。

It accepts a reference to a variable as the parameter.

This means that any changes that the function makes to the parameter (eg, $errors = "Error!") will affect the variable passed by the calling function.

摘星┃星的人 2024-10-09 12:02:38

这意味着在错误位置传递的变量将被被调用的函数修改。请参阅了解详细信息。

It means that the variable passed in the errors position will be modified by the called function. See this for a detailed look.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文