Welcome Guest, Not a member yet? Register   Sign In
the error undefined index userid
#1

[eluser]searain[/eluser]
Code:
<?php
    if ($_SESSION['userid'] < 1)
    {
        echo 'Please log in';
    }
    else
    {
        echo 'Hello Friend!';
    }
?&gt;
These codes are fine in php.

But in codeIgniter, if I put these codes in, I could get the error
undefined index userid.

I have to use such as
Code:
&lt;?php
    if (isset($_SESSION['userid']) and ($_SESSION['userid'] < 1))
    {
        echo 'Please log in';
    }
    else
    {
        echo 'Hello Friend!';
    }
?&gt;
To check if $_SESSION['userid'] isset?

Is this codeIgniter rule which overwrite the php general practice of the variable (or just session), or just the set up of the sample codes I was using.
#2

[eluser]pistolPete[/eluser]
Did you try the first example with
Code:
error_reporting(E_ALL);
?
It should display the warning as well...
#3

[eluser]searain[/eluser]
Thanks!

Missed that part.

What is the best practice in CI coding?

In developing, we should always make the codes error proof for E_ALL level error reporting? Or just make the codes error proof for "E_ALL & ~E_NOTICE" level error reporting?

My guess is CI wants us to write codes error proof for E_ALL level error reporting. But many codes in CI were not error proof for E_ALL level error reporting, even in developing, the error proof were set up to E_ALL and no error reported.

For example, the codes like this

Code:
&lt;?php
    if ($_SESSION['userid'] < 1)
    {
        echo 'Please log in';
    }
    else
    {
        echo 'Hello Friend!';
    }
?&gt;

We saw a lot in the CI sample codes. As long as the human developer knows that the $_SESSION['userid'] is already set, the code will be fine or error_reporting E_ALL. But to make the codes human error free for E_ALL error reporting, we need to write like this

Code:
&lt;?php
    if (isset($_SESSION['userid']) and ($_SESSION['userid'] < 1))
    {
        echo 'Please log in';
    }
    else
    {
        echo 'Hello Friend!';
    }
?&gt;

I didn't see too many codes written this way.

Thanks!
#4

[eluser]jedd[/eluser]
I think it's a laudable - and quite achievable - goal to produce code that is absent syntactical or parsing errors.




Theme © iAndrew 2016 - Forum software by © MyBB