Welcome Guest, Not a member yet? Register   Sign In
[Thanks All For Advice] Checking if isset is correct?
#1

(This post was last modified: 09-11-2016, 04:46 PM by wolfgang1983.)

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 }?>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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

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 )
Reply
#4

(This post was last modified: 09-09-2016, 03:47 PM by wolfgang1983.)

(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.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#5

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.
Reply
#6

(This post was last modified: 09-10-2016, 11:59 AM by PaulD.)

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

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. 
Reply
#8

(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?
Reply
#9

(This post was last modified: 09-11-2016, 11:22 PM by Wouter60.)

(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.
Reply
#10

(This post was last modified: 09-12-2016, 02:53 AM by PaulD.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB