2

I have the following code:

$a=$_POST['dsaghgjjkhfdsfdsfsdfdsjhkhkhgj'];

if(isset($a))
{
    echo"exists";
}
else
{
    echo"does not exist";
}

echos the value "does not exist", HOWEVER when i apply trim to the $_POST variable,

$a=trim($_POST['dsaghgjjkhfdsfdsfsdfdsjhkhkhgj']);

if(isset($a))
{
    echo"exists";
}
else
{
    echo"does not exist";
}

the code will echo "exists". Why does passing in a non-existing $_POST variable to trim() magically makes it exist?

Kenneth .J
  • 1,433
  • 8
  • 27
  • 49
  • You have no check in your code at all that checks whether `$_POST['dsaghgjjkhfdsfdsfsdfdsjhkhkhgj']` exists or not. So how can you say so that `$_POST['dsaghgjjkhfdsfdsfsdfdsjhkhkhgj']` is magically made into existance when used with `trim()`? You have neither showed that it did not exist nor that it does exist. You're obviously mixing things here, perhaps you're irritated by the fact that `trim()` returns a string, always? Perhaps you're running to a conclusion too fast while not checking the basics? – hakre Jan 05 '14 at 11:15
  • Recommended reading: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Jan 05 '14 at 11:17

4 Answers4

5

This is exactly what happens, step by step.

When you refer to $_POST['...'] in the second code snippet this notice is issued:

Notice: Undefined index: ... on line ...

You don't get the notice as your error_reporting level does not include E_NOTICE. The intermediate $_POST['...'] is evaluated NULL and trim(NULL) returns an empty string. So $a is assigned an empty string.

If you prepend error_reporting(E_ALL) and ini_set("display_errors", "on") to your script, you will see the actual errors/warnings/notices issued.

gd1
  • 11,300
  • 7
  • 49
  • 88
3

Because trim returns a string, regardless of its inputs, see http://ch2.php.net/manual/en/function.trim.php.

php > var_dump(NULL);
NULL
php > var_dump(trim(NULL));
string(0) ""

A string, even if it is empty, is declared as "defined". This is why isset returns true then.

pdu
  • 10,295
  • 4
  • 58
  • 95
  • it doesn't explain what happens – zerkms Jan 05 '14 at 10:43
  • Yes it does. trim creates a string from its inputs. – pdu Jan 05 '14 at 10:44
  • It *did not* explain what happens. Originally (when I posted my comment) your answer didn't have anything but code. Also it makes sense to mention implicit casting, since it's caused not by returning a string, but by casting a `null` – zerkms Jan 05 '14 at 10:44
  • This does not explain how $_POST['...'] results in NULL. – gd1 Jan 05 '14 at 10:55
  • It does not "result" in NULL. Accessing a var which is not defined automatically is "NULL" when accessing it. – pdu Jan 05 '14 at 12:41
2

This is what isset does:

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

In your first example, the $_POST variable doesn’t exist. Non-existing variables have the value null:

A variable is considered to be null if:

  • it has been assigned the constant NULL.
  • it has not been set to any value yet.
  • it has been unset().

So $a does also equal null. Although $a exists, it has the value null, so isset returns false.

In your second example, null is passed to trim, which returns an empty string:

var_dump(trim(null)); // string(0) ""

So $a does also equal an empty string. And since $a exists and has value other than null, isset returns true.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • I would also remark that a E_NOTICE is issued when referring to non existing variables and array indices. – gd1 Jan 05 '14 at 11:00
1

You could try empty() function. trim() is returning an empty string != NULL:

$a=trim($_POST['dsaghgjjkhfdsfdsfsdfdsjhkhkhgj']);

if(empty($a))
{
    echo"does not exist";
}
else
{
    echo"exists";
}

From php.net:

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
geedubb
  • 4,048
  • 4
  • 27
  • 38