I have this doubt regarding an if that I am doing in php, what happens is that I receive a POST variable but since it does not have anything yet it is considered NULL, but that turns out to be 0 for php since in my if I am conditioning the $variable <= 0 only this is null.
$CantidadInicial = $_POST["cantidad_inicial"];
if($CantidadInicial <= 0){
var_dump($CantidadInicial);
echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal.fire("La cantidad de la caja no puede ser 0!","preuba con cotra cantidad !","");';
echo '}, 500);</script>';
}
in var_dump($CantidadInicial);
me it throws me the value is NULL only that the condition should not be met
Assuming you are implementing php in a web form. The first thing I usually validate is that the variable
$_POST
exists and contains the index to be treated, I do this validation as follows:output:
In addition, this also helps you to capture and validate any other error in the test and development scenarios that you handle, database connections; to FTP Etc...
It is also important that you understand why you are receiving a false/positive that is due to the data cast for verification:
php will cast values to types as close as possible before issuing a warning or fatal error.
I detail the following information found on the site in English: League
in your case the null will be cast to false and the 0 will be cast to false; therefore the false part is equal to false.