3

Code from Robin Nixon book:

<?php
if (isset($_POST['name'])) $name = $_POST['name'];
else $name = '(enter your name)';
echo <<<_END
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
    Your name is $name<br />
    <form method = 'post' action = 'count.php'>
        What's your name?
        <input type='text' name='name' />
        <input type='submit' />
    </form>
    </body>
</html>
_END
?>

In the second line we check is variable set or not with isset(). In the third line we have a condition: if it's not set, the script prints 'enter your name'. That's what I do not understand: I open this page - it prints:

Your name (Enter your name) What is your name? (and the submission form)

Did not enter nothing at all, then hit "send" - it prints:

Your name is (and does NOT print "Enter your name") What's your name? (and the submission form)

I didn't enter anything but the function said that the variable was set to a value other than NULL. Why? If it passes an empty value, then why use it? Why just not use empty? But in all programs I see solution like this. Why do we have to use isset() function there? What don't I understand?

user229044
  • 232,980
  • 40
  • 330
  • 338
9Algorithm
  • 1,258
  • 2
  • 16
  • 22
  • `var_dump($_POST['name'])` will show you that it's a `string(0)` (empty/zero-length string). ALL values coming out of _GET/_POST are strings, regardless of their actual content. – Marc B Nov 19 '12 at 20:41
  • possible duplicate of [Why check both isset() and !empty()](http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty) – Gajus Apr 10 '14 at 07:11

5 Answers5

4

The isset function is used to check if the text-field <input type='text' name='name' /> has any data submitted via POST which is captured in $_POST['name'].

When you submit the form, the $_POST['name'] is blank, or an empty string. Hence, you see Your name is What's your name? (and the submission form), as isset evaluated to true for an empty string.

If you want to check for the emptiness of the text-field, you should use empty() function, which checks for both isset and well as if the field is blank/empty.

  if (!empty($_POST['name'])) $name = $_POST['name'];
  else $name = '(enter your name)';
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
0

change isset to !empty. You are retrieving an empty string from the post variable.

if (!empty($_POST['name'])) $name = $_POST['name'];
else $name = '(enter your name)';

OR to be more concise:

$name=empty($_POST['name'])?'(enter your name)':$_POST['name'];
Jon Hulka
  • 1,259
  • 10
  • 15
0

That's because an empty string (which is sent by the browser to the server) is considered a valid value to cause a variable to be "set", so isset() returns true for it.

You want to check for empty() instead, if you want to make sure anything was entered.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

As everyone else has said, empty() could work, but your question is why isset instead of empty.

Empty will return TRUE for anything that evaluates as FALSE, such as 0, 0.0, "0", so could lead to unintended consequences.

For completeness, you should probably check first if it's set, and then if it's empty, and handle accordingly.

I think lots of PHP coders also get in the habit of using isset() to check if the key exists in an array, so using isset comes naturally.

You'll note that in another example in that chapter, Nixon has special cases to handle the empty strings.

Community
  • 1
  • 1
ernie
  • 6,356
  • 23
  • 28
  • 1
    empty will not work in all cases. entering `0` or a single space would count as "empty" but could quite easily be wrong. – Marc B Nov 19 '12 at 20:43
  • @MarcB Ack, had accidentally pressed "Enter" when you noted that (hence the trailing q). I wanted to explain the difference between isset and empty a bit, as well as explain why lots of people get in the habit of using isset – ernie Nov 19 '12 at 20:54
0

Make sure the form is already submitted by adding a name to the submit button and testing it first. Like this:

<input type='submit' name="submit" />

<?php
if (isset($_POST['submit'])) {
if (isset($_POST['name']))  $name = $_POST['name'];
....
}
?>
Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • 1
    better to do `if ($_SERVER["REQUEST_METHOD"] == 'POST')` than check for the presence/absnce of a field. – Marc B Nov 19 '12 at 20:43