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?
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) - Sh4dowempty
works on non-existent variables. Live and learn. But my skepticism everybody wants to deny such a jobempty
.. =) - Yakovlev Andreyisset()
. 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 withempty()
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 ...) - Indifferentempty
accepts the reference, and the empty reference does not issue awarning
'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