Why use isset if(isset($_POST['submit'])) {}?

Hello,I can not understand why use of f-tion isset() in if(isset($_POST['submit'])) { some actions }
At first thought to not produce an error,if processing occurs on a separate page.But since FALSE and NULL are included,it turns out that no error can not be.
Then what is this check for existence of a variable needed?If false includes null?
July 9th 19 at 10:46
4 answers
July 9th 19 at 10:48
Solution
As I understand it, the fact is that not all the gold glitters. And your variable is not necessarily explicitly true. A variable may exist and empty value, or even false. What will you do then? Here's an example that shows it. You just independently verify.
$var_test = 0;
var_dump($var_test);
if($var_test){
 echo('does Not work');
}

if(isset($var_test)){
echo('will Work');
}
Thank you.Now I understand - rylan commented on July 9th 19 at 10:51
: This is misleading.
For this there is is_null().
The main purpose of isset is to check if a variable exists in principle, for example, in order not to override. - Clementina_Jacobs commented on July 9th 19 at 10:54
: Well, if I understand correctly how it works for me.
Is input(button) which when clicked will define a blank line ( I don't know what returns the button,but apparently an empty string).And it turns out that without isset() in the if will be FALSE (because FALSE accepts an empty string too) and a script inside the if will not run.And if you use isset () will return TRUE and it will work.
And is_null() in the manual will also return FALSE, and again, the script will not run.
Maybe I'm confused and don't understand - rylan commented on July 9th 19 at 10:57
: Yes you have it confused. You just read for yourself on the Internet about isset and empty. What you ask does not require is_null(). Although that will happen if that variable is NULL, then isset will not know about its existence, because opredelyaet whether a variable is set and non-NULL


$var_test = NULL;
var_dump($var_test);
if($var_test){
echo('does Not work');
}

if(isset($var_test)){
echo('will Not work');
}

if(is_null($var_test)){
echo('will work');
} - Juana95 commented on July 9th 19 at 11:00
: Try this:
$var = false;
unset($var);
echo isset($var) ? $var: 'no exist';
echo $var? $var: 'no exist'; - Clementina_Jacobs commented on July 9th 19 at 11:03
In addition, I have received an answer here)
Quote from php.net In the case of an uninitialized variable is called the error of level E_NOTICE, except in the case of adding elements to uninitialized array. For the detection of initialization of a variable can be used by the language construct isset(). End quote.

In your case the output of E_NOTICE is suppressed by settings php on a different server with different settings of the emergence of heaps of warnings can become an unpleasant surprise and, in some cases, even break the script. In very active scripts is inevitable, the expansion of the log - rylan commented on July 9th 19 at 11:06
July 9th 19 at 10:50
Solution
In order to see if the script was handed $_POST['submit'], to perform some specific actions.
if(isset($_POST['submit'])) {
 echo "submit\n";
}
echo "Hi";

When you run this script will be displayed:
Hi
If the run is initialized from the form that was sent to $_POST['submit'], then it displays:
submit
Hi

php.net/manual/ru/function.isset.php
July 9th 19 at 10:52
isset() — determines whether a variable is set and non-NULL.

But if the variable does not exist, then null will not return, but raises an error.
For this there is is_null().
The main purpose of isset is to check if a variable exists in principle, for example, in order not to override.

$var = false;
unset($var);
echo isset($var) ? $var : 'no exist';
echo $var? $var : 'no exist';
July 9th 19 at 10:54
and you make it so

<?php
$a = null;
$b = false;
$c = 0;
if(isset($a)) {
 echo "1";
 echo "<br /-->";
var_dump($a);
 echo "<br>";
}

if(isset($b)) {
 echo "2";
 echo "<br>";
var_dump($b);
 echo "<br>";
}
if(isset($c)) {
 echo "3";
 echo "<br>";
var_dump($c);
 echo "<br>";
}
if(isset($d)) {
 echo "4";
 echo "<br>";
var_dump($d);
 echo "<br>";
}


check

Find more questions by tags PHP