Question:
Standard introduction : I'm just starting to learn php, don't swear for a stupid question, etc. Code from the book by 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 (), we check whether the variable is set or not. In the third, we have a condition: if it is not set, then we display "(Not entered)". Here's what I don’t understand: I open the page – displays:
Your name is (Not entered) What is your name? (and a form to submit)
I do not enter anything, I press "send" – it displays:
Your name is What is your name? (and does NOT output "not entered") (and the form to submit)
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 misses a null value, then why use it? Why not use empty ? But in all programs I see just such a check. What do I not understand?
Answer:
Yes, after the 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 as you pointed out, why not take empty
?
The difference is that empty checks the value of an existing variable for emptiness.
Therefore, a double condition would be optimal here:
if (isset($_POST['name']) && !empty($_POST['name']))