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

PhpFatal Error

Php Problem Overview


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);
}

Php Solutions


Solution 1 - Php

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, use trim($name) == false. From empty documentation.

Solution 2 - Php

You mean

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

though incidentally you really mean

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

Solution 3 - Php

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 

Solution 4 - Php

for WORDPRESS:

instead of:

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

should be:

if (!get_option('smth'))

Solution 5 - Php

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.

Solution 6 - Php

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.

Solution 7 - Php

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

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

by

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

Solution 8 - Php

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.

Solution 9 - Php

Another scenario where this error is trigered due syntax error:

ucwords($variable) = $string;

Solution 10 - Php

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;
    }

Solution 11 - Php

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.

Solution 12 - Php

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 ==.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncfischerView Question on Stackoverflow
Solution 1 - PhprolfenView Answer on Stackoverflow
Solution 2 - PhpchaosView Answer on Stackoverflow
Solution 3 - PhpTigerTigerView Answer on Stackoverflow
Solution 4 - PhpT.ToduaView Answer on Stackoverflow
Solution 5 - PhpmiddusView Answer on Stackoverflow
Solution 6 - PhpMehdi KaramoslyView Answer on Stackoverflow
Solution 7 - PhpDiegoView Answer on Stackoverflow
Solution 8 - PhphusnixsView Answer on Stackoverflow
Solution 9 - Phptomelin5View Answer on Stackoverflow
Solution 10 - PhpShaakirView Answer on Stackoverflow
Solution 11 - Phpuser12845085View Answer on Stackoverflow
Solution 12 - PhpHammad KhanView Answer on Stackoverflow