奇怪的PHP错误:'不能在写上下文中使用函数返回值'

cfi*_*her 179 php fatal-error

我收到了这个错误,我无法做出它的头或尾.

确切的错误消息是:

致命错误:第48行/home/curricle/public_html/descarga/index.php中的写入上下文中无法使用函数返回值

第48行是:

if (isset($_POST('sms_code') == TRUE ) {
Run Code Online (Sandbox Code Playgroud)

谁知道发生了什么?

PS这是完整的功能,如果有帮助:

function validate_sms_code() {

    $state = NOTHING_SUBMITED;

    if (isset($_POST('sms_code') == TRUE ) {
        $sms_code = clean_up($_POST('sms_code'));
        $return_code = get_sepomo_code($sms_code);

        switch($return_code) {

          case 1:
            //no error
            $state = CORRECT_CODE;
            break;

          case 2:
            // code already used
            $state = CODE_ALREADY_USED;
            break;

          case 3:
            // wrong code
            $state = WRONG_CODE;
            break;

          case 4:
            // generic error
            $state = UNKNOWN_SEPOMO_CODE;
            break;

          default:
            // unknown error
            $state = UNKNOWN_SEPOMO_CODE;
            throw new Exception('Unknown sepomo code: ' . $return_code);
            break;
        }

    } else {
        $state = NOTHING_SUBMITED;
    }
    dispatch_on_state($state);
}
Run Code Online (Sandbox Code Playgroud)

小智 490

在函数返回上使用empty时也会发生这种情况:

!empty(trim($someText)) and doSomething()
Run Code Online (Sandbox Code Playgroud)

因为empty不是一个函数而是一个语言构造(不确定),它只需要变量:

对:

empty($someVar)
Run Code Online (Sandbox Code Playgroud)

错误:

empty(someFunc())
Run Code Online (Sandbox Code Playgroud)

从PHP 5.5开始,它支持的不仅仅是变量.但如果您在5.5之前需要它,请使用trim($name) == false.从空文档.

  • @Shredder - 这是因为`empty()`实际上不是一个函数; 它是一种语言结构,如`echo`.PHP解析器以不同方式处理语言结构.在"empty"的情况下,它将参数视为变量,而不是要评估的内容,因此如果您尝试传递函数,它会中断.至少那是我对它的理解.更多信息[这里](http://www.phpknowhow.com/basics/language-constructs-vs-built-in-functions/)和[here](http://www.php.net/manual/en/ function.empty.php). (15认同)
  • 我没有,我的同事得到了这个错误.随后出现了混乱,直到我检查了文档 - 从PHP 5.5开始,empty()将接受函数和变量的返回.http://us3.php.net/empty (2认同)

cha*_*aos 111

你的意思是

if (isset($_POST['sms_code']) == TRUE ) {
Run Code Online (Sandbox Code Playgroud)

虽然顺便说一句,你的意思是

if(isset($_POST['sms_code'])) {
Run Code Online (Sandbox Code Playgroud)

  • 我意识到我已经很晚了,但它也依赖于PHP版本,对吧?我很确定这在后来的PHP版本中是合法的,比如5.6,但我认为它不适用于5.3 (5认同)
  • ...或`if(isset($ _ POST ['sms_code'])=== TRUE){`:-) (3认同)

Tig*_*ger 22

if (isset($_POST('sms_code') == TRUE ) {
Run Code Online (Sandbox Code Playgroud)

将此行更改为

if (isset($_POST['sms_code']) == TRUE ) {
Run Code Online (Sandbox Code Playgroud)

你正在使用parentheseis(),$_POST但你想要方括号[]

:)

要么

if (isset($_POST['sms_code']) && $_POST['sms_code']) { 
//this lets in this block only if $_POST['sms_code'] has some value 
Run Code Online (Sandbox Code Playgroud)


T.T*_*dua 13

对于WORDPRESS:

代替:

if (empty(get_option('smth')))
Run Code Online (Sandbox Code Playgroud)

应该:

if (!get_option('smth'))
Run Code Online (Sandbox Code Playgroud)


mid*_*dus 11

正确的语法(最后你的括号丢失):

if (isset($_POST['sms_code']) == TRUE ) {
                            ^
Run Code Online (Sandbox Code Playgroud)

ps你不需要 == TRUE 部分,因为已经返回BOOLEAN(true/false).


Meh*_*sly 5

这种情况可能会在多种情况下发生,下面列出了一些众所周知的情况:

// calling empty on a function 
empty(myFunction($myVariable)); // the return value of myFunction should be saved into a variable
// then you can use empty on your variable
Run Code Online (Sandbox Code Playgroud)

// 使用括号访问数组的元素,括号用于调用函数

if (isset($_POST('sms_code') == TRUE ) { ...
// that should be if(isset($_POST['sms_code']) == TRUE)
Run Code Online (Sandbox Code Playgroud)

当我们尝试增加如下函数的结果时,也可能会触发此操作:

$myCounter = '356';

$myCounter = intVal($myCounter)++; // we try to increment the result of the intVal...
// like the first case, the ++ needs to be called on a variable, a variable should hold the the return of the function then we can call ++ operator on it.
Run Code Online (Sandbox Code Playgroud)