繁体   English   中英

如何正确调用PHP函数?

[英]How to properly call a PHP function?

我目前有一个PHP函数,需要从其他文件中调用以填充Javascript变量。 我可以很好地填充Javascript变量。 但是,当调用PHP函数时,我收到一条错误消息,指出

使用未定义的常量getEstGrid-在第29行的phpcom / db / estimatesCom.php </ b>中假定为“ getEstGrid”。

这是我的包含文件,具有以下功能:

<?php include 'phpcom/db/estimatesCom.php';?>

这是我调用该函数的地方:

<?php echo getEstGrid($resultsE); ?>

功能码:

function getEstGrid($resultsE){//<--This is the variable passed to the function
  if ($resultsE->num_rows > 0) { //<--- Check to see if the variable is greater than zero
    $rows = array(); //<--- Create an empty array calls "rows"
        while ($r = $resultsE->fetch_assoc()){//<--- While the database records are being returned create a variable called "r" and assign DB record.
           $rows[] = $r; //<-- assign each DB record row to the array.
        }
        $data = array('Estimates' => $rows);//<--- Create a variable an assing the array to it.
  }
  //header("Content-Type: application/json;charset=utf-8");
  echo json_encode($data, true);//<--- Endode the "data" variable to a JSON format.
  return getEstGrid;//<--- Return the created fucntion.
}

如果有人可以提供帮助,我将非常感激。

只需从函数中删除此return语句即可:

return getEstGrid;//<--- Return the created fucntion.

你为什么用那个? 不需要。

此外,如果您使用的echo在你的功能,你不需要使用echo ,同时调用你的函数打印输出。

您不必返回该函数即可在另一个文件中使用它。

相反,只需从函数中返回所需的值即可:return json_encode(...

当您尝试返回“ getEstGrid”时,php会查找具有该名称的常量,该常量不存在。

我会改变这一点:

echo json_encode($data, true);//<--- Endode the "data" variable to a JSON format.
return getEstGrid;//<--- Return the created fucntion.

这样:

return json_encode($data, true);

这样,该函数仅返回数据,您可以选择是否回显它,或者直接使用它执行任何其他操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM