PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 31, 2021

[FIXED] What causes the error "Can't use method return value in write context" in this Codeigniter 3 application?

 December 31, 2021     codeigniter, codeigniter-3, php     No comments   

Issue

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?


Solution

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.



Answered By - TimBrownlaw
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing