Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message: Fatal error: Can't use function return > value in write context in

Tags:

php-5.3

I am trying to run some code from a book. There appears to be a problem with the code.

Here is the error message:

Fatal error: Can't use function return value in write context in /Applications/MAMP/htdocs/Eclipse-Workspace/simpleblog/test.php on line 24

Here is the code referenced in the message (starting on line 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 />"; 
        } 

I would appreciate any help. Please let me know if I need to provide any more information


Thanks a lot guys. That was very fast. The solution works great.

The problem is that the empty() function needs to be applied only to direct variables.

For future reference: The code is from 'PHP for Absolute Beginners' by Jason Lengstorf (2009), pages 90-91, Chapter 3, $_SESSION

corrected code:

    //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 />"; 
} 
like image 839
ntc Avatar asked Nov 16 '10 08:11

ntc


1 Answers

In short: The empty() function only works directly on variables

<?php
empty($foo); // ok
empty(trim($foo)); // not ok

i'd say, for the course of getting further with that book, just use a temporary variable

so change:

if (!empty(trim($_POST['username'])) 

to

$username = trim($_POST['username']);
if(!empty($username)) { 
     //....
like image 109
edorian Avatar answered Sep 30 '22 18:09

edorian