There are two files: form.html and cookie_set.php

form.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form name="entry" action="cookie_set.php" method="POST"> <fieldset> <legend>Вводите только цифры и буквы</legend> Имя: <input type="text" name="user"> Пароль: <input type="password" name="pass"> <br> <input type="submit" value="Отправить"> </fieldset> </form> </body> </html> 



cookie_set.php

 <?php if (isset($_POST["user"])) echo "да<br>"; if (isset($_POST["pass"])) echo "да<br>"; ?> 


I do not enter any form element, click send and display "yes" 2 times. in fact, $ _POST ["user"] and $ _POST ["pass"] should not be set unless values ​​are entered into them, but the isset () function considers differently. Why?
But there is a script in which if you do not select the answer option, isset () will say that the element of the $ _POST array is not set. Below are two files
Two more files:

form2.php

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="action2.php" method ="POST"> <fieldset> <legend>Каким языком программирования является PHP?</legend> Структурным <input type="radio" name="type" value="structure"><br> Процедурным <input type="radio" name="type" value="procedure"><br> Объектно-ориентированным <input type="radio" name="type" value="OOP"><br> <input type="submit" value="Отправить"> </fieldset> </form> </body> </html> 


action2.php

 <?php if (isset($_POST['type'])) { $answer = $_POST['type']; } else { $answer = NULL; } echo "answer = $answer<br>"; if ($answer != NULL) { if ($answer == "OOP") echo "Правильно!<br>"; else echo "Неправильно!<br>"; } else { echo "Вы не выбрали вариант.<br>"; } 
  • action="cookie_set.php" and doc.php code? - Rustam Gimranov
  • I made a mistake in specifying the php file in question. The script is called cookie_set.php - JustLearn
  • @RustamGimranov fixed - JustLearn
  • one
    Check if (empty ($ _POST ['user "])) - axmed2004
  • @ axmed2004 thanks, but I want to figure out why it does not work with isset () - JustLearn

1 answer 1

In the first case, when sending an empty form, var_dump($_POST); will output:

 array(2) { ["user"]=> string(0) "" ["pass"]=> string(0) "" } 

As you can see ["user"] = "" is an empty string.

The isset function returns TRUE if the variable is defined and its value is non- NULL . In your case, it is equal to the empty string.

Function empty - checks if the variable is empty. A variable is considered empty if it does not exist or its value is:

  1. "" (empty string)
  2. 0 (integer)
  3. 0.0 (floating point number)
  4. "0" (string)
  5. Null
  6. FALSE
  7. array () (empty array)

In the second case: checkbox and radio , if they are not selected, they are not transmitted to the server, and therefore these variables are not in the $ _POST global array. The same is true for the file input field and the presence of a variable in $ _FILES.

That is why most often with the help of empty check for the presence and non-emptiness.

 if (!empty($_FILES['image']['name'])) { ... } 

empty does not generate a warning if the variable does not exist. This means that empty actually the exact equivalent of

 !isset($var) || $var == false 
  • Thank you)) But I don’t understand why then for input type = "radio", isset () will assume that $ _POST ["..."] does not exist. I will now add another small listing to the question - JustLearn
  • Rustam Gimranov, added - JustLearn