8

Ok the problem is that when i use the trim function doesnt work but when i run the code without the trim function its working, but not properly working(the form accepts whitespaces)

<?php
session_start();
unset($_SESSION['username']);

 if (isset($_SESSION['username']))
    {echo "You are in already";}

 else if ($_SERVER['REQUEST_METHOD'] == 'POST')
 {      

 if (!empty(trim($_POST['username'])) && !empty(trim($_POST['email'])))
    {
        $uname = htmlentities($_POST['username']);
        $email = htmlentities($_POST['email']);
  $_SESSION['username'] = $uname;

            echo "THANKS: " . $uname . "<br />";
    }
 else { echo "fill the goddemn field"; }


  } else { ?>

<form action="index.php" method="post">
 <label for="username">USERNAME:</label>
 <input type="text" name="username" />
 <label for="E-MAIL">E-mail:</label>
 <input type="text" name="email" />
 <input type="submit" value="Enter" />
</form>


<?php    } ?>

I tried the manual http://php.net/manual/en/function.trim.php but it was hard to read and I didn't figure out anything.

phpisuber01
  • 7,585
  • 3
  • 22
  • 26
user2300068
  • 97
  • 1
  • 1
  • 5
  • 5
    You cannot pass the result of `trim()` into `empty()` directly. Turn on error reporting, and you will see errors related to it. `empty()` requires a _variable_ as its argument, so you must first store the `trim($_POST[])` into vars, then test via `empty()` – Michael Berkowski Jun 07 '13 at 16:09
  • 2
    `error_reporting(E_ALL); ini_set('display_errors', 1);` _always_ do this when developing code. – Michael Berkowski Jun 07 '13 at 16:10
  • 1
    Side note: Seems counter intuitive to `unset` a variable then immediate check if it's set with `isset`. – phpisuber01 Jun 07 '13 at 16:11
  • 2
    Looking [at the `empty()` docs](http://us2.php.net/manual/en/function.empty.php), as of PHP 5.5 it will _finally_ support an arbitrary expression rather than choking on anything that isn't a real variable. – Michael Berkowski Jun 07 '13 at 16:13
  • possible duplicate of [Can't use method return value in write context](http://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context) – Michael Berkowski Jun 07 '13 at 16:16
  • @phpisuber01: simple way of bypassing the 'logged in' check to force the not-logged branch. it's almost certainly there just for debugging. – Marc B Jun 07 '13 at 16:17
  • note: prefer using `===`. `empty` is a horrible method (one of the many bad design choices of the language) – Karoly Horvath Jun 07 '13 at 16:47

1 Answers1

18

As the PHP manual says:

empty — Determine whether a variable is empty

In your case, trim is a function call, not a variable.

If you really want to do your if statement inline, you can use something like:

if (!empty($var=trim($_POST['username'])) && !empty($var=trim($_POST['email'])))

But a better implementation should be:

$username = array_key_exists('username', $_POST) ? trim($_POST['username']) : null;
$email = array_key_exists('email', $_POST) ? trim($_POST['email']) : null;

if (!empty($username) && !empty($email))
{
    (...)
Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
  • 4
    The function `empty()` will not accept any arbitrary expressions (below PHP 5.5), and therefore function calls, won't work, **as well** as variable assignment, rendering your inline example invalid. – losnir Mar 31 '14 at 09:56
  • Is there a way to quickly check if a function return is not null – Miguel Stevens Jun 26 '15 at 08:03