【问题标题】:Cannot make out this error code [closed]无法找出此错误代码[关闭]
【发布时间】:2016-07-10 21:46:57
【问题描述】:

我尝试让脚本工作。我支付了 45.00 美元,因为上传时此行一直出现错误。

if(empty($this->GetData('UserImage'))) {

执行这段代码,我得到消息:

致命错误:无法在写入上下文中使用方法返回值 /home/wwwjcpsocials/public_html/files/functions.php 在第 162 行

【问题讨论】:

  • 致命错误:无法在第 162 行的 /home/wwwjcpsocials/public_html/files/functions.php 的写入上下文中使用方法返回值
  • 没有足够的代码/信息来提供解决方案。你可能不想展示你的完整代码,因为你已经付费了,但是我们不能用一行代码做很多事情。
  • 公共函数 GetImage($UserID = false, $ImageWidth = false, $ImageHeight = false) { if(empty($this->GetData('UserImage'))) { echo ''; } else { $img = base64_encode(stripslashes($this->GetData('UserImage'))); echo '';
  • 请将您的代码放在编辑 stackoverflow.com/posts/36180943/edit 中,而不是 cmets 中。并且使用 public 意味着一个类

标签: php panel


【解决方案1】:

发生这种情况是因为您在调用函数/类时无法检查 returnecho 是否为空。示例:

例如,这将返回一个致命错误:

function isEmpty() {
    return "";
}

// This will return: Fatal error: Can't use function return value in write context
if(empty(isEmpty())) {
    echo "The function returned an empty string";
}

你应该如何检查函数是否以正确的方式返回值?示例:

function isEmpty() {
    return "";
}

// Because you have an variable
// You can check if the variable is empty.
$var = isEmpty();
if(empty($var)) {
    echo "The variable is empty.";
}

要解决您的问题,您应该在调用if 语句之前添加一个新变量,例如$userImage = $this->GetData('UserImage');

然后将if(empty($this->GetData('UserImage'))) {替换为:if(empty($userImage)) {

注意:这取决于您在服务器上运行的 PHP 版本。在 PHP 5.5 之前,empty() 只支持变量;其他任何事情都会导致解析错误。

希望对你有帮助。

【讨论】:

  • 取决于 PHP 版本,3v4l.org/sTFiJ.
  • @chris85 啊哈,不知道,感谢您的反馈! :-)
【解决方案2】:
$var = $this->GetData('UserImage');
if(empty($var)) {

【讨论】:

  • 仅代码答案不是很有用。您应该解释问题,以便用户知道为什么要使用您的代码,否则明天可能会遇到类似问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2012-04-11
  • 2021-07-30
  • 2013-10-06
  • 1970-01-01
  • 2021-11-07
相关资源
最近更新 更多