Php isset do you need in validation form?
I'm trying to figure out if isset is needed during form processing, when I check $ _REQUEST ["input_name"], if no value is passed, it doesn't cry about it, and php doesn't throw an error if you try to access an array element which doesn't exist .... I can use if ($ _ REQUEST ["input_name"]) .. how about "empty" even when I can use if ()
Thnks
source to share
I would not recommend using $_REQUEST
superglobal for input form input unless you are testing the form. Use $_GET
or $_POST
instead unless you have a really good reason.
Also, isset()
and array_key_exists()
do the same trick about array keys, albeit array_key_exists()
clearer in the context of arrays.
I recommend using:
error_reporting(E_ALL); //E_ALL - All errors and warnings
within your development environment, as it might reveal where better methods can be applied, such a failure to declare variables before using them, etc.
source to share
There are different types of errors. Checking a variable that is not set only triggers a notification. Your bug report will probably ignore them. Best used isset
when you want to check if a variable has been set, although it has its bugs .
Doing only what you are doing above, for example, if $_REQUEST["input_name"]
is the string "0", it will evaluate to false. It is also not recommended to use $ _REQUEST to begin with as it can be influenced by things like cookies, etc. and is usually a code smell for bad architecture.
source to share
Generally, at least for testing purposes, set the error message to E_ALL (all errors and warnings) either in your php.ini or in code using error_reporting (E_ALL); ( Look also at the addition of E_STRICT .) It's better to get an obvious error notification before something subtle goes wrong, "catch it all the way".
Avoid using $ _REQUEST, which is too vague (it includes GET, POST, and cookie values), and use $ _POST or $ _GET if that's what you really mean, and validate with isset($_POST["input_name"])
The short answer is "Yes". :)
source to share