错误消息:致命错误:无法在写入上下文中使用函数return> value

ntc*_*ntc 6 php-5.3

我试图从一本书中运行一些代码.代码似乎有问题.

这是错误消息:

致命错误:第24行的/Applications/MAMP/htdocs/Eclipse-Workspace/simpleblog/test.php中的写入上下文中不能使用函数返回值

这是消息中引用的代码(从第24行开始)

if (!empty(trim($_POST['username'])) 
        && !empty(trim($_POST['email']))) { 
        // Store escaped $_POST values in variables 
            $uname = htmlentities($_POST['username']); 
            $email = htmlentities($_POST['email']); 

            $_SESSION['username'] = $uname; 

            echo "Thanks for registering! <br />", 
                "Username: $uname <br />", 
                "Email: $email <br />"; 
        } 
Run Code Online (Sandbox Code Playgroud)

我将不胜感激任何帮助.如果我需要提供更多信息,请告诉我


非常感谢你们.那非常快.解决方案很有效.

问题是empty()函数只需要应用于直接变量.

供将来参考:代码来自Jason Lengstorf的"PHP for Absolute Beginners"(2009),第90-91页,第3章,$ _SESSION

更正的代码:

    //new - Created a variable that can be passed to the empty() function
    $trimusername = trim($_POST['username']);

    //modified - applying the empty function correctly to the new variable 
    if (!empty($trimusername) 
    && !empty($trimusername)) { 

    // Store escaped $_POST values in variables 
    $uname = htmlentities($_POST['username']); 
    $email = htmlentities($_POST['email']); 

    $_SESSION['username'] = $uname; 

    echo "Thanks for registering! <br />", 
        "Username: $uname <br />", 
        "Email: $email <br />"; 
} 
Run Code Online (Sandbox Code Playgroud)

edo*_*ian 6

简而言之:该empty()函数仅直接用于变量

<?php
empty($foo); // ok
empty(trim($foo)); // not ok
Run Code Online (Sandbox Code Playgroud)

我会说,对于进一步阅读这本书的过程,只需使用一个临时变量

所以改变:

if (!empty(trim($_POST['username'])) 
Run Code Online (Sandbox Code Playgroud)

$username = trim($_POST['username']);
if(!empty($username)) { 
     //....
Run Code Online (Sandbox Code Playgroud)