Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm trying to get my head around the various validation functions in PHP. As a beginner, I'm trying to simplify things as much as possible. So, based on the information on page 49 in the book I have this general rule:

 

When validating a text or textarea use !empty(); when using a radio button or select use isset().

 

 

My questions:

 

1. Is this a good general rule?

2. Can !empty() be used instead of isset()? This would certainly make things more simple.

3. Since isset() can return TRUE on an empty string (p. 49), why is it used in the calculator example for the distance (p. 88)?

 

The top matrix on the PHP site is certainly helpful. Still, the plethora of validation options does make this a bit confusing, at least for me. So, any simplification is helpful.

 

Thanks.

Link to comment
Share on other sites

When validating a text or textarea use !empty(); when using a radio button or select use isset().

1. Is this a good general rule?

 

Yes, it's a good general rule. However, it's actually best to always call isset() on any type first, to confirm that the variable exists before referencing it. Then you could call !empty() to make sure the text input isn't an empty string. Alternatively, you can check the length of the text value.

 

2. Can !empty() be used instead of isset()? This would certainly make things more simple.

 

Nope. Two different functions that do different things. empty() will give you notice errors if it's used on a variable that doesn't exist.

 

3. Since isset() can return TRUE on an empty string (p. 49), why is it used in the calculator example for the distance (p. 88)?

 

Good question! I don't use empty() on page 88 because I use is_numeric(), which is more exacting. So the isset() tests that they have values, any values (including empty strings), but is_numeric() tests for numeric values, which an empty string wouldn't qualify as.

Link to comment
Share on other sites

 Share

×
×
  • Create New...