Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use method return value in write context

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?

like image 847
Roman Avatar asked Jul 18 '11 08:07

Roman


2 Answers

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.

like image 162
Francois Deschenes Avatar answered Oct 11 '22 16:10

Francois Deschenes


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'; }
like image 42
Sajjad Ashraf Avatar answered Oct 11 '22 17:10

Sajjad Ashraf