Skip to content
Advertisement

What causes the error “Can’t use method return value in write context” in this Codeigniter 3 application?

I am working on a Social Network application with Codeigniter 3, Ion-Auth and Bootstrap 4. You can see the Github repo HERE.

When editing a user’s profile, I check if there is a new user photo (avatar) in the edit form. If it is, I use it, if not I use (keep) the one already existing in the users table (file path is application/controllers/Auth.php):

$new_file = $this->upload->data('file_name');
$this->file_name = (isset($new_file) && !empty($new_file)) ? $new_file : $user->avatar;

The above code works fine.

However, I need update the user’s photo within the session and for this purpose I added just below $this->file_name = (isset($new_file) && !empty($new_file)) ? $new_file : $user->avatar:

if (isset($new_file) && !empty($new_file)) {
    $this->session->userdata('user_avatar') = $new_file;
}

The above if statement causes the error “Can’t use method return value in write context”.

What am I doing wrong?

Advertisement

Answer

See Why check both isset() and !empty() regarding the use of isset and empty. You do not need to use both.

if (isset($new_file) && !empty($new_file)) {
    $this->session->userdata('user_avatar') = $new_file;
}

Here you should be using $this->session->set_userdata('user_avatar',$new_file) ( as per the user guide).

So it becomes

if (!empty($new_file)) {
    $this->session->set_userdata('user_avatar',$new_file);
}

You do not need to even consider using isset() as you are defining $new_file in your code above, so it will always be “set” i.e isset($new_file) will always be true.

But what do you intend to do in the case where $new_file is empty? That’s a question you need to consider.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement