回显上一个函数的返回值

xoefb8l8  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(349)
function report_test() {

$color = "blue";
$name = "John";

mobile_test($color, $name);
echo $columnid;
}

function mobile_test($color, $name) {

-snipped Insert MySQL Query-
$columnid = 5;
return $columnid;
}

希望上面的例子描述了我正在尝试做的事情。由于其他各种原因,全局化变量columnid在这种情况下不是一种选择。我也不想改变 mobile_test($color, $name) 功能。我发现,如果我回显函数,mysql insert查询将运行两次,这意味着两组相同的结果被输入到数据库中。
还有别的办法吗?

mctunoxg

mctunoxg1#

我想你是想 echo 出去 return 价值来自 mobile_test() 内部 report_test() . 最简单的方法就是 echo 出去 $columnid 伊尼斯德 mobile_test() . 当你打电话的时候 mobile_test()report_test() ,的 echo 语句,并输出值:

function mobile_test($color, $name) {
  ...
  $columnid = 5;
  echo $columnid;
  return $columnid;
}

还可以使用php的短标记语法(如 <?= mobile_test(); ?> ),假设要回显 return 直接使用函数,而不使用它做任何其他事情。
请注意,如果您在 return 价值( return $columnid; ),您可以直接将此值用作 report_test() 功能:

if (mobile_test($color, $name) === 5) {
   echo "The column ID is 5" /* This line will be triggered */
}
x4shl7ld

x4shl7ld2#

您可以这样做:

function report_test() {

$color = "blue";
$name = "John";

$columnid = mobile_test($color, $name);
echo $columnid;
}

您需要存储 mobile_test() 作为一个新变量。这不需要改变 mobile_test()

相关问题