[Thanks All For Advice] Checking if isset is correct? |
Question: On my view file default.php I would like to know if I have set the isset correct? It's just a new way of loading views that I am trying
On my controllers I load views like Login.php PHP Code: public function index() Dashboard.php PHP Code: public function index() How ever if the navbar does not exist on my login views array but does on dashboard views array On my view default.php I would like to know what is the better way of setting isset PHP Code: <?php foreach ($views as $view) {?>
There's only one rule - please don't tell anyone to go and read the manual. Sometimes the manual just SUCKS!
Read this Article:
PHP isset() vs empty() vs is_null() What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
The Ternary is great for deciding on which of two possible values to return.
But in this case you are deciding whether or not to take one possible action. Code: <?php In this particular case the ternary is not needed and a simple if statement is cleaner.
isset is not always good enough, for instance suppose the $view['navbar'] is set in a model or library call which returns FALSE if not needed. Then:
PHP Code: <?php Whereas PHP Code: <?php So it entirely depends on the usage of the variable being tested. For the OP example, I would use PHP Code: <?php if (!empty($view['navbar'])) $this->load->view($view['navbar']); ?>
If you (auto)load the array helper, you could also use the element() function.
Usage: PHP Code: $navbar = element('navbar',$view,FALSE); (09-11-2016, 05:08 PM)JayAdra Wrote:Hence the use of element(). This will not throw an error, but you can decide yourself what value you want if the array key doesn't exist.(09-10-2016, 11:50 AM)PaulD Wrote:
No it does not throw an error. From the docs:
Quote:No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false. http://php.net/manual/en/function.empty.php Also: Quote:__isset() is triggered by calling isset() or empty() on inaccessible properties. http://php.net/manual/en/language.oop5.o...ject.isset |
Welcome Guest, Not a member yet? Register Sign In |