CodeIgniter Forums
[Thanks All For Advice] Checking if isset is correct? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Thanks All For Advice] Checking if isset is correct? (/showthread.php?tid=66127)



[Thanks All For Advice] Checking if isset is correct? - wolfgang1983 - 09-09-2016

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 }?>



RE: Checking if isset is correct? - JayAdra - 09-09-2016

Should be fine. Does it work? If so, why are you asking?


RE: Checking if isset is correct? - InsiteFX - 09-09-2016

Read this Article:

PHP isset() vs empty() vs is_null()


RE: Checking if isset is correct? - wolfgang1983 - 09-09-2016

(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.


RE: Checking if isset is correct? - dave friend - 09-09-2016

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.


RE: Checking if isset is correct? - PaulD - 09-10-2016

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']); ?>



RE: Checking if isset is correct? - Wouter60 - 09-11-2016

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. 



RE: [Thanks All For Advice] Checking if isset is correct? - JayAdra - 09-11-2016

(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?


RE: [Thanks All For Advice] Checking if isset is correct? - Wouter60 - 09-11-2016

(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.


RE: [Thanks All For Advice] Checking if isset is correct? - PaulD - 09-12-2016

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.overloading.php#object.isset