Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isset() or !empty() functions on all variables in your views?

Tags:

php

views

cakephp

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?

like image 613
baker Avatar asked Dec 21 '09 03:12

baker


2 Answers

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.

like image 164
Christian C. Salvadó Avatar answered Sep 23 '22 14:09

Christian C. Salvadó


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.

like image 30
deceze Avatar answered Sep 23 '22 14:09

deceze