tags:

views:

311

answers:

6

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?

+9  A: 

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.

CMS
+1  A: 

Personally, I use isset out of habit, negating is !something I use alot.

Anthony Forloney
+1  A: 

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.

deceze
I'm fully agree with you with gracefully catch error
silent
+1  A: 

To answer the question, no.

  1. If the value can't be null in the model, then I don't.
  2. If it's for errors and emptysets, then its better to redirect to add pages and 404 not founds.
  3. When I do use it in a view, its usually at a high level, so that I don't have to write either very often.
  4. #3 involves often involves careful design of your model. SO using Isset and !empty is o excuse not to work on those models.
  5. Items that are optional, seem to often also be in one to many relationships, for those foreach loops are more effective than checking each value.
stevenf
A: 

when it is a choice between this or that

when it is not is set, it is empty <=> when it is not empty, it is set

but when it is set, it may be empty... an may not..........

so this is the differnece

Robust Solution
A: 

When you do something like $this->set(compact('posts', 'tags', 'categories')); from the controller, only variables that are not null get sent to the view. So if you had previously done $categories = $this->Categories->find('all', ...); and it returned no results, then a $categories will not exist in your view. So to answer your question, in the view, I simply do:

<?php if (isset($categories)): ?>
    <ul>
        <?php foreach ($categories as $category): ?>
            <li>{output category information here}</li>
        <?php endforeach; ?>
    </ul>
<?php else: ?>
    <p>No categories</p>
<?php endif; ?>

Empty values also don't equate to true, so you can do things like:

<dl>
    <dt>Title</dt>
    <dd><?php echo $category['Category']['title']; ?></dd>
    <dt>Description</dt>
    <dd>
        <?php if ($category['Category']['description']): ?>
            <?php echo $category['Category']['description']; ?>
        <?php else; ?>
            None
        <?php endif; ?>
    </dd>
</dl>

In short I only use isset() to avoid errors about variables not being set.

I rarely use empty() and I managed just fine without it in other programming languages too.

deizel