从函数PHP获取返回值
如何回显PHP中另一个函数中调用的函数返回的值 例如,如果我有这样的函数:从函数PHP获取返回值,php,function,Php,Function,如何回显PHP中另一个函数中调用的函数返回的值 例如,如果我有这样的函数: function doSomething($var) { $var2 = "someVariable"; doSomethingElse($var2); } function doSomethingElse($var2) { // do anotherSomething if($anotherSomething) { echo "the function ran"; re
function doSomething($var) {
$var2 = "someVariable";
doSomethingElse($var2);
}
function doSomethingElse($var2) {
// do anotherSomething
if($anotherSomething) {
echo "the function ran";
return true;
}
else {
echo "there was an error";
return false;
}
}
我想回显第一个函数中第二个函数的回显。原因是第二个函数在失败时可以生成字符串,而第一个函数不能
那么,如何从第二个函数中输出返回的值呢?创建一个包含要返回的值的数组,然后返回该数组
function doSomethingElse($var2) {
// do anotherSomething
if($anotherSomething) {
$response['message'] = "the function ran";
$response['success'] = TRUE;
}
else {
$response['message'] = "there was an error";
$response['success'] = FALSE;
}
return $response;
}
在你的其他职能中
$result = doSomethingElse($var2);
echo $result['message'];`
对于
$anotherSomething
,您将收到未定义变量的通知。声明在哪里?echo doSeomthingElse($var2)
?这似乎是最好的解决方案,实际上,让我试一试。谢谢你,丹。