Hi I'm using CakePHP and I'm wondering do you guys puts isset() or !empty() around all of your variables in the views? Or should I depend on the data validation? What would be the suggested solution?
I think you should know the differences between isset
and empty
and use the one that fulfills your needs.
empty
will return FALSE
if the variable has a non-empty and non-zero value.
The following values are considered to be empty:
""
(an empty string)0
(0 as an integer)"0"
(0 as a string)NULL
FALSE
array()
(an empty array)var $var;
(a variable declared, but without a value in a class)On the other hand isset
will return FALSE
if the variable does not exist or has been unset with unset()
, or the variable has been set to NULL
.
That's a pretty broad question. It depends on whether you can expect the variable to always be present or if there might reasonably be cases where it isn't. If, according to your program structure, a certain variable should always be present at this point in the program, you should not check for its existence. This way you'll get a nice warning when something screws up and you know something went wrong. If, OTOH, you expect the variable to sometimes be absent, you need to check for this case to gracefully catch the error that would otherwise result.
Furthermore, the choice between isset
and !empty
depends on whether you mean "is set and not null
" or "is set and contains something that's not considered false
". That's a small but sometimes important difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With