Standard entry : Just starting to learn php, do not swear for a stupid question, etc. Code from the book of Robin Nixon.

<?php if (isset($_POST['name'])) $name = $_POST['name']; else $name = '(Не введено)'; echo <<<_END <html> <head> <title>Test</title> </head> <body> Вас зовут $name<br /> <form method = 'post' action = 'count.php'> Как вас зовут? <input type='text' name='name' /> <input type='submit' /> </form> </body> </html> _END ?> 

Question: In the second line, using isset (), check whether the variable is set or not. In the third we have a condition: if not set, then output "(Not entered)". Here's what I do not understand: I open the page - displays:

Your name (Not entered) What is your name? (and form to send)

I enter nothing, click "send" - displays:

Your name What is your name? (and does NOT display "not entered") (and the form to send)

That is, I do not enter anything, but the function considers that the variable was set to a value other than NULL . Why? If it skips a null value, then why use it? Why not use empty ? But in all programs I see just such a check. What don't I understand?

  • five
    @Andrey Yakovlev, empty does not necessarily exist , it works quite normally with undefined (it will be NULL). if (isset ($ a) and! empty ($ a)) // tautology, if A and (A and B) Here it is enough !empty . Also, this question should be poked with the muzzle of many new ones, for it is at least Nubian, but well decorated and adequately set, therefore a plus sign) - Sh4dow
  • 2
    @ Sh4dow, but looked into the documentation, really empty works on non-existent variables. Live and learn. But my skepticism everybody wants to deny such a job empty .. =) - Yakovlev Andrey
  • 2
    Logically, what is the challenge? The task is to find out if the data has been sent. That is why isset() . And the fact that the data came empty - well, sometimes it happens. In this case, we will be able to display some informational message to the user, saying "the field must be filled for something." And with empty() we have no idea what exactly happened, which means we will not be able to react correctly. ZY if the task from the textbook has caused a lot of questions - it means she did her job ...) - Indifferent
  • four
    @ Indifferent in general in such cases it is customary to send another hidden-field) $ action =! Empty ($ _ POST ['action'])? $ _POST ['action']: ''; if ($ action == 'save') {// and here isset is often simply not needed, because // the set of fields is known} But this is another story) And yes, @Yakovlev Andrey, here the topic is actually deeper. empty accepts the reference, and the empty reference does not issue a warning 'and, but simply contains NULL. A rather unobvious feature, but it can be used. For a snack: $ a = null; var_dump (isset ($ a)); // bool (false) - Sh4dow

5 answers 5

Yes, after submission $ _POST ['name'] got the right to exist ( isset ).
After all, an empty string is also a value.
And if there is a value, then there is a variable.
But you correctly noticed why not take empty ?
And the difference is that empty checks for the void values ​​of the existing variable.
Therefore, a double condition would be optimal here:

 if (isset($_POST['name']) && !empty($_POST['name'])) 
  • It’s a very long time for each variable to write this, use foreach for the entire array of post or get and also trim'te and! empty any $ value available in the key array ($ key) - ferrari

My answer may seem silly, but:

  1. I personally have a habit from the university - to frame the operators running in the " if-then-else " if-then-else into blocks. In this case, "{" and "}".
  2. When learning PHP4 , I sometimes had a problem with variables, which was solved by the configuration file settings: php.ini or httpd.conf .

I think in paragraph 2 there is more probability of solving the problem. Good luck!

PS: The operator "@" has not been canceled yet. As I recall, it is needed in order to prevent errors during the initial initialization of a variable. Something like this.

  • Bad habit: I personally have a habit from university to frame operators that run in an if-then-else construction into blocks. In this case, "{" and "}". - AseN
  • one
    The operator "@" is not relevant here. It simply hides the display of errors and notices. This is a "bad" operator. Even for your project. - lampa 8:36 pm
 if( strlen($_POST['name'])>2 ){ //если длина $_POST['name'] больше 2 выполняется тут } 
  • one
    @ Dima Grebynyak, cool. And I will take and put 2 spaces. - ModaL
  • If passed an array, and if the encoding is left? - lampa

There is documentation. Type comparison table in PHP

     // переменные которые ждёте: $name = "(не введено)"; $age = "(не указано)"; // получаем из пост foreach ($_POST as $key => $value) { $clear_val = trim ($value); // тут может быть: html_spec_char, sql_spec_char итд if (!empty($clear_val)) { $$key = $clear_val; } } print "Имя: $name, возраст $age.";