Fatal error can t use function return value in write context in

After making an update to one of my WordPress plugins, when trying to load plugin settings page, I get the …

thumb

After making an update to one of my WordPress plugins, when trying to load plugin settings page, I get the following error message:

Fatal error: Can’t use function return value in write context in /public_html/domain-name.com/wp-content/plugins/rss-feed-icon-for-specificfeedscom/inc/php/functional.php on line 19

For WordPress users, I recommend reading another article about this error.

What causes this error

This error message is self explanatory. It means that we are trying to use function to return value in write context. It also tells us where exactly this error occurred. To solve this issue, follow the steps below.

How to solve it

  1. Open the file that is mentioned in the error message. In my case this is the functional.php file, which is placed in the /public_html/domain-name.com/wp-content/plugins/rss-feed-icon-for-specificfeedscom/inc/php/ directory.

  2. Find the line specified in the error message. In my case this is the 19th line.

  3. Here you can see the code that causes the error. That’s how it looks in my case:

if ( ! empty( get_option( 'my_option' ) ) {

Note! You can see something similar to this but with isset() or another PHP function, and with custom function instead of WordPress function get_option(). For example:

if ( ! empty( myFunction( $myVariable ) ) {

or:

if ( isset( $_POST( 'my_option' ) == TRUE ) ) {

All I had to do to solve this error was to change the code above to the following:

if ( ! get_option( 'my_option' ) )

This code does exactly the same thing but in more elegant way. Also we can make a variable and then use it in the our if construction:

$my_option = get_option( 'my_option' );
if ( ! empty( $my_option ) {

Conclusion

That’s it, you’re done. Now your plugin should work without this error. So simple isn’t it?

If you are having trouble fixing this problem with the instructions above, but are being able to solve this problem with any another method please describe it in the comment section below. Thanks!

If this article has helped you solve the problem then please leave a comment :smiley:

Thanks for reading!

Error Description:

Weird PHP error: ‘Can’t use function return value in write context’

Solution 1:

  • This also happens when using empty on a function return:
!empty(trim($someText)) and doSomething()
click below button to copy the code. By — php tutorial — team
  • because empty is not a function but a language construct (not sure), and it only takes variables:
empty($someVar)

click below button to copy the code. By — php tutorial — team
  • Since PHP 5.5, it supports more than variables. But if we need it before 5.5, use trim($name) == false.

Solution 2:

if (isset($_POST('sms_code') == TRUE ) {

click below button to copy the code. By — php tutorial — team
  • change this line to
if (isset($_POST['sms_code']) == TRUE ) {

click below button to copy the code. By — php tutorial — team
  • we are using parentheseis () for $_POST but we wanted square brackets []
  • OR
if (isset($_POST['sms_code']) && $_POST['sms_code']) { 
//this lets in this block only if $_POST['sms_code'] has some value 

click below button to copy the code. By — php tutorial — team

Solution 3:

  • This can happen in more than one scenario, below is a list of well known scenarios :
// calling empty on a function 
empty(myFunction($myVariable)); // the return value of myFunction should be saved into a variable
// then you can use empty on your variable
// using parenthesis to access an element of an array, parenthesis are used to call a function
if (isset($_POST('sms_code') == TRUE ) { ...
// that should be if(isset($_POST['sms_code']) == TRUE)

click below button to copy the code. By — php tutorial — team
  • This also could be triggered when we try to increment the result of a function like below:
$myCounter = '356';

$myCounter = intVal($myCounter)++; // we try to increment the result of the intVal...
// like the first case, the ++ needs to be called on a variable.
click below button to copy the code. By — php tutorial — team

Solution 4:

  • PHP Fatal error: Can’t use function return value in write context in …/wp-content/themes/customizr/inc/czr-init.php on line 441

if ( ! function_exists( ‘czr_fn_has_social_links’ ) ) {
function czr_fn_has_social_links() {
return !empty( czr_fn_get_opt(‘tc_social_links’) );
}
click below button to copy the code. By — php tutorial — team

265 votes

12 answers

Get the solution ↓↓↓

I’m getting this error and I can’t make head or tail of it.

The exact error message is:

Fatal error: Can’t use function return
value in write context in
/home/curricle/public_html/descarga/index.php
on line 48

Line 48 is:

if (isset($_POST('sms_code') == TRUE ) {

What could be going on here?

Here’s the full function:

function validate_sms_code() {

    $state = NOTHING_SUBMITED;

    if (isset($_POST('sms_code') == TRUE ) {
        $sms_code = clean_up($_POST('sms_code'));
        $return_code = get_sepomo_code($sms_code);

        switch($return_code) {

          case 1:
            //no error
            $state = CORRECT_CODE;
            break;

          case 2:
            // code already used
            $state = CODE_ALREADY_USED;
            break;

          case 3:
            // wrong code
            $state = WRONG_CODE;
            break;

          case 4:
            // generic error
            $state = UNKNOWN_SEPOMO_CODE;
            break;

          default:
            // unknown error
            $state = UNKNOWN_SEPOMO_CODE;
            throw new Exception('Unknown sepomo code: ' . $return_code);
            break;
        }

    } else {
        $state = NOTHING_SUBMITED;
    }
    dispatch_on_state($state);
}

2021-11-22

Write your answer


318

votes

Answer

Solution:

This also happens when using empty on a function return:

!empty(trim($someText)) and doSomething()

because empty is not a function but a language construct (not sure), and it only takes variables:

Right:

empty($someVar)

Wrong:

empty(someFunc())

Since PHP 5.5, it supports more than variables. But if you need it before 5.5, usetrim($name) == false. From empty documentation.


443

votes

Answer

Solution:

You mean

if (isset($_POST['sms_code']) == TRUE ) {

though incidentally you really mean

if (isset($_POST['sms_code'])) {


306

votes

Answer

Solution:

if (isset($_POST('sms_code') == TRUE ) {

change this line to

if (isset($_POST['sms_code']) == TRUE ) {

You are using parentheseis () for$_POST but you wanted square brackets []

:)

OR

if (isset($_POST['sms_code']) && $_POST['sms_code']) { 
//this lets in this block only if $_POST['sms_code'] has some value 


916

votes

Answer

Solution:

for WORDPRESS:

instead of:

if (empty(get_option('smth')))

should be:

if (!get_option('smth'))


813

votes

Answer

Solution:

Correct syntax (you had a missing parentheses in the end):

if (isset($_POST['sms_code']) == TRUE ) {
                            ^

p.s. you dont need== TRUE part, because BOOLEAN (true/false) is returned already.


520

votes

Answer

Solution:

This can happen in more than one scenario, below is a list of well known scenarios :

// calling empty on a function 
empty(myFunction($myVariable)); // the return value of myFunction should be saved into a variable
// then you can use empty on your variable

// using parenthesis to access an element of an array, parenthesis are used to call a function

if (isset($_POST('sms_code') == TRUE ) { ...
// that should be if(isset($_POST['sms_code']) == TRUE)

This also could be triggered when we try to increment the result of a function like below:

$myCounter = '356';

$myCounter = intVal($myCounter)++; // we try to increment the result of the intVal...
// like the first case, the ++ needs to be called on a variable, a variable should hold the the return of the function then we can call ++ operator on it.


882

votes

Answer

Solution:

The problem is in the() you have to go[]

if (isset($_POST('sms_code') == TRUE)

by

if (isset($_POST['sms_code'] == TRUE)


621

votes

Answer

Solution:

I also had a similar problem like yours. The problem is that you are using an old php version. I have upgraded to PHP 5.6 and the problem no longer exist.


532

votes

Answer

Solution:

Another scenario where this error is trigered due syntax error:

ucwords($variable) = $string;


283

votes

Answer

Solution:

i also ran into this problem due to syntax error. Using «(» instead of «[» in array index:

   foreach($arr_parameters as $arr_key=>$arr_value) {
        $arr_named_parameters(":$arr_key") = $arr_value;
    }


656

votes

Answer

Solution:

This error is quite right and highlights a contextual syntax issue. Can be reproduced by performing any kind «non-assignable» syntax. For instance:

function Syntax($hello) { …. then attempt to call the function as though a property and assign a value…. $this->Syntax(‘Hello’) = ‘World’;

The above error will be thrown because syntactially the statement is wrong. The right assignment of ‘World’ cannot be written in the context you have used (i.e. syntactically incorrect for this context). ‘Cannot use function return value’ or it could read ‘Cannot assign the right-hand value to the function because its read-only’

The specific error in the OPs code is as highlighted, using brackets instead of square brackets.


20

votes

Answer

Solution:

Can be cause by wrong operator, =, when it should be ==

if(mysql_num_rows($result) = 1)
    return $result;
else
    return false;

This code throws this error

Note that = is assignment operator and not comparison operator. Fix is to change = to ==.


Share solution ↓

Additional Information:

Date the issue was resolved:

2021-11-22

Link To Source

Link To Answer
People are also looking for solutions of the problem: ftp_put(): can’t open that file: no such file or directory

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

Понравилась статья? Поделить с друзьями:
  • Fatal error attempting to save in maya что делать
  • File uploads require push access to this repository как исправить
  • Fatal error allowed memory size opencart
  • File uploads are disabled cs go как исправить
  • File upload error unable to create a temporary file in unknown on line 0