The following if condition is causing an error:
if( !empty ($this -> session -> userdata( 'user_id') ) )
{
$user_id = $this -> session -> userdata( 'user_id' );
}
else
{
$error = "ID doesn't exist";
}
I get following error:
Fatal error: Can't use method return value in write context in (line number)
How can I fix it?
Replace the first if
statement with if( ! $this->session->userdata('user_id') )
.
The empty
function check if a variable is empty but userdata
is a function. Additionally, for your information, according to the CodeIgniter documentation, the userdata
function returns FALSE
if the item doesn't exist, which means that if user_id
doesn't exist in your session, the code in the else
will be executed.
Function empty() cannot be used in this way. It expects reference argument, but not a direct function return. A function return is not considered a variable, which can be passed by reference.
$test = $this->session->userdata('user_id');
if(!empty($test))
$user_id = $test;
else
$error = "ID doesn't exist";
If you are using the sessions library of codeigniter then you don't need to check your data codeigniter already does that for you. if $this -> session -> userdata( 'user_id')
is empty or not set then it will be returned as false. So just a code something like that
$user_id = $this->session->userdata('user_id');
if(!$user_id){ }else{ $error = 'ID doesn't exist'; }
if( ($this -> session -> userdata( 'user_id') > 0)
it is more better instead of if( !$this -> session -> userdata( 'user_id') )
because sometimes it will make error