tags:

views:

1633

answers:

7

Hi, I trying to understand if a isset is required 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 out an error if you are trying to access a array item which doesn't exist....i can use if($_REQUEST["input_name"]).. what about "empty" even in those cases i can use if()

THnks

+1  A: 
if($_REQUEST["input_name"])

will throw a notice (error) if "input_name" doesn't exist, so isset() is recommended.

Ian
isset() does suppress the warning, but $_REQUEST is a sucky way to go about capturing form input in most cases.
karim79
+3  A: 

There are different type of error levels. Checking a variable that is not set only throws a notice. Your error reporting is probably set to ignore those. It is best practice to always use isset when you want to check if a variable has been set, although it does have its gotchas.

Doing only what you are doing above, for example, if $_REQUEST["input_name"] is the string "0", it will evaluate to false. Also it is not a good idea to use $_REQUEST to begin with, as it can be affected by stuff like cookies and such and it's usually a code smell for bad architecture.

Paolo Bergantino
+1; it's important to check whether or not the value is set, rather than inadvertently rely on PHP coercing the value into a boolean properly
Rob
A: 

It is generally recommended to check whether a variable exists before using it.

I believe that configurations that run at E_ALL error reporting levels will report when the script attempts to use an array item that does not exist, and if your code will be used on such configurations, it is best to make sure that such variables and array items exist before using them.

However, you can suppress the error messages by prepending function calls with @. I would still use isset (), though.

Rishabh Mishra
+3  A: 

using $_REQUEST is pretty much a hack. You should be using $_POST or $_GET (depending on what you are doing) and you should use isset().

Every book I've read on PHP seems to say that.

+3  A: 

I wouldn't recommend using the $_REQUEST superglobal for capturing form input, unless you're testing a form. Use $_GET or $_POST instead, unless you have a really good reason.

Also, isset() and array_key_exists() both do the same trick with regard to array keys, although array_key_exists() is clearer in an arrays context.

I recommend using:

error_reporting(E_ALL); //E_ALL - All errors and warnings

within your development environment, as that can expose where better practices might be applied, such failure to declare variables before they are used, etc.

karim79
Magic values aren't fun. You can just do `error_reporting(E_ALL);` - `E_ALL` is a valid constant.
Samir Talwar
Yea, E_ALL is clearer and also more reliable in case the constant is changed in the future (no pun intended).
Calvin
-1 for 30719. Use the define.
jmucchiello
Changed to the more fun E_ALL
karim79
+1  A: 

Generally, at least for testing, set error reporting to E_ALL (all errors and warnings) either in your php.ini or in code using error_reporting(E_ALL); (Look into adding E_STRICT too.) Better to get an obvious notice about an error up front, than to have something subtle go wrong that you don't catch till later.

Avoid using $_REQUEST, which is too vague (it includes GET, POST AND cookie values), and use the $_POST or $_GET if those are what you really mean, and do check with isset($_POST["input_name"])

The short answer is "Yes." :)

Paul Kroll
A: 

It is not about the error reporting. I was thinking more in terms of using "if" statement rather than isset. That was my question. Sorry if i pointed you guys in the wrong direction. THnks for all your replies.

coool
Where would you be using isset() other than inside an if statement?
sirlancelot
if($_POST["input_name"]) !!!
coool