6

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 />"; 
} 
ntc
  • 257
  • 1
  • 4
  • 9

2 Answers2

6

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)) { 
     //....
edorian
  • 38,542
  • 15
  • 125
  • 143
  • Thanks a lot. This works. I've updated the question with the corrected code in case anyone else is using this book and runs into the problem. – ntc Nov 16 '10 at 09:30
  • 1
    Thank you so much for asking and answering this question with the corrected code. I'm new to PHP and to programming, and when I run across a typo in the book I pretty much always figure it's just me not getting it. It's such a relief to put the right things in and see it work! –  Feb 15 '11 at 21:28
  • I think it should be noted, that this behaviour only affects PHP versions < 5.5 as stated at [php.function.empty](http://php.net/manual/en/function.empty.php) – ochitos Aug 13 '15 at 05:07
3

Exactly your example is mentioned at the manual

Note:

empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

Use a temporary variable, or just test against "empty string"

if (trim($foo) !== '') {
    // Your code
}
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • Thanks Sebastian. Is there any way I could have inferred from the error message that the problem was with this function? I'm not very good at understanding them at this point. – ntc Nov 16 '10 at 09:27
  • Some methods takes parameters as reference and so they only accepts variables, because they are the only one, which can get passed as reference. "pass by reference" usually means, that the function *maybe* want to write to this variable (which also affects the variable value outside the function). And that is, what the message tries to tell: "empty" (whyever) cannot write to the return value of a function, just to variables. – KingCrunch Nov 16 '10 at 13:16