CodeIgniter Forums

Full Version: [Thanks All For Advice] Checking if isset is correct?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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() 
{
    
    $this
->data['views'][] = array(
        'header' => 'common/header',
        'footer' => 'common/footer',
        'content' => 'common/login'
    );

    $this->load->view('common/default'$this->data);


Dashboard.php

PHP Code:
public function index() 
{
    
    $this
->data['views'][] = array(
        'header' => 'common/header',
        'footer' => 'common/footer',
        'navbar' => 'common/navbar'
        'content' => 'common/dashboard'
    );

    $this->load->view('common/default'$this->data);


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) {?>
    
    <?php $this->load->view($view['header']);?>
    
    <?php isset($view['navbar']) ? $this->load->view($view['navbar'])  '';?>
    
    <div class="container">

        <?php $this->load->view($view['content']);?>

    </div>

    <?php $this->load->view($view['footer']);?>

<?php }?>
Should be fine. Does it work? If so, why are you asking?
(09-09-2016, 04:39 AM)JayAdra Wrote: [ -> ]Should be fine. Does it work? If so, why are you asking?

Yes works fine just was not sure best way of doing it.
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
if(isset($view['navbar']){
   $this->load->view($view['navbar']);
}

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
$var 
FALSE;
echo isset(
$var) ? 'Set' 'Not Set';
// output: Set
?>

Whereas

PHP Code:
<?php
$var 
FALSE;
echo empty(
$var) ? 'Not Set' 'Set';
// output: Not Set
?>

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);
if ( 
$navbar$this->load->view($navbar);
//Explanation: if the array element $view['navbar'] exists, it returns it's value; otherwise it returns FALSE. 
(09-10-2016, 11:50 AM)PaulD Wrote: [ -> ]
PHP Code:
<?php if (!empty($view['navbar'])) $this->load->view($view['navbar']); ?>

Will that not throw a warning if the key doesn't exist in the array though? Hence the use of isset?
(09-11-2016, 05:08 PM)JayAdra Wrote: [ -> ]
(09-10-2016, 11:50 AM)PaulD Wrote: [ -> ]
PHP Code:
<?php if (!empty($view['navbar'])) $this->load->view($view['navbar']); ?>

Will that not throw a warning if the key doesn't exist in the array though? Hence the use of isset?
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.
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