gpt4 book ai didi

php - 如何在代码的其他地方调用在 PHP 函数中声明的变量

转载 作者:行者123 更新时间:2023-12-04 16:45:54 26 4
gpt4 key购买 nike

function my_function() {
$var1 = "123";
$var2 = "abc";
return $var1;
}

有时我想使用 $var2但调用 my_function()将简单地返回函数返回的内容( $var1 )。

有什么技巧可以找回 $var2数据 ?也许 my_function($var2) ..

问候。

最佳答案

只需传递参数 by reference ,例如

$var1 = "";
$var2 = "";

function my_function(&$var1, &$var2) {
//^ ^ See here
$var1 = "123";
$var2 = "abc";

}

my_function($var1, $var2);
//Now you can use $var1 and $var2 for you next function with the new values.

如果您想了解更多关于一般引用的信息,请参阅手册: http://php.net/manual/en/language.references.php

关于php - 如何在代码的其他地方调用在 PHP 函数中声明的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30101028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com