Welcome Guest, Not a member yet? Register   Sign In
Using (@) error suppression on Undefined Variables
#1

[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!
#2

[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 @.
#3

[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.
#4

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

[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;




Theme © iAndrew 2016 - Forum software by © MyBB