CodeIgniter Forums
Using (@) error suppression on Undefined Variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Using (@) error suppression on Undefined Variables (/showthread.php?tid=22448)



Using (@) error suppression on Undefined Variables - El Forum - 09-10-2009

[eluser]Gwarrior[/eluser]
Is the proper way to keep undefined variables in a page from erroring to use the @ operator?

For example, on my view:

Code:
<?php if (@$logged_in == TRUE) { ?>
<p>Welcome back, &lt;?=$name;?&gt;!</p>
&lt;?php } ?&gt;

If logged_in is not supplied (which looks at the session userdata and marks the variable as a boolean value in the controller), is it okay to do this?

Thanks!


Using (@) error suppression on Undefined Variables - El Forum - 09-10-2009

[eluser]bigtony[/eluser]
It's VERY bad practice to use undefined variables, and therefore the use of @ should only be needed on rare occassions.

If you are using CI sessions then the variable won't be undefined in any case:
Code:
$logged_in = $this->session->userdata('logged_in');
The above will set $logged_in to FALSE if the actual logged_in value (in the session) is undefined. So you then won't need to use @.


Using (@) error suppression on Undefined Variables - El Forum - 09-10-2009

[eluser]cahva[/eluser]
..or use:
Code:
if (isset($logged_in) AND $logged_in == TRUE)
But as bigtony said, thats definately information that belongs to session, so use that.


Using (@) error suppression on Undefined Variables - El Forum - 09-10-2009

[eluser]Gwarrior[/eluser]
Can you reference session data from a view or do you have to pass it down through a variable?


Using (@) error suppression on Undefined Variables - El Forum - 09-10-2009

[eluser]bigtony[/eluser]
[quote author="Gwarrior" date="1252592242"]Can you reference session data from a view or do you have to pass it down through a variable?[/quote]
Yes, you can reference it in a view, something like this:
Code:
&lt;body&gt;
    <p>
        &lt;?php if ($this->session->userdata('logged_in') == TRUE): ?&gt;
            You are logged in
        &lt;?php else: ?&gt;
            You are NOT logged in
        &lt;?php endif; ?&gt;
    </p>
&lt;/body&gt;