Error message: Fatal error: you cannot use the function return> value in the context of an entry in

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

Here is the error message:

Fatal error: you cannot use the return function value in the context of the entry in / Applications / MAMP / htdocs / Eclipse -Workspace / simpleblog / test.php on line 24

Here is the code indicated in the message (starting at 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 be grateful for any help. Please let me know if I need to provide more information.


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

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

For future reference: Code from "PHP for Absolute Beginners" by Jason Lengstorf (2009), pages 90-91, chapter 3, $ _SESSION

fixed 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 />"; } 
+6
source share
2 answers

In short: the empty() function only works directly with variables

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

I would say that to continue working with this book, just use a temporary variable

so change:

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

to

 $username = trim($_POST['username']); if(!empty($username)) { //.... 
+6
source share

Exactly your example is mentioned in the manual.

Note:

empty () only checks for variables since everything else will result in a parsing error. In other words, the following will not work: empty (trim ($ name)).

Use a temporary variable or just check the "empty string"

 if (trim($foo) !== '') { // Your code } 
+3
source share

All Articles