2 ways to fix: Can’t use method return value in write context

Have you ever used a function call in empty() for an if condition to perform some checking? Then you might come up with an PHP fatal error saying Can’t use method return value in write context. I am explaining here why did the error come and 2 ways to write them correctly.

Reason to error: Can’t use method return value in write context

The reason why do we get the error while using such type of code in PHP versions prior to 5.5 is that empty() needs to access value by reference (in order to check whether that reference points to something that exists), and PHP before 5.5 didn’t support references to temporary values returned from functions.

In short: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). And the same is true for isset().

We can write the same code in 2 ways to let them work:

And using the second way and by removing NOT operator, we can write our Codeigniter Session Example as follow:

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.