Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
}
like image 814
cfischer Avatar asked Oct 07 '09 16:10

cfischer


9 Answers

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.

like image 86
rolfen Avatar answered Oct 29 '22 00:10

rolfen


You mean

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

though incidentally you really mean

if (isset($_POST['sms_code'])) {
like image 36
chaos Avatar answered Oct 28 '22 23:10

chaos


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 
like image 32
TigerTiger Avatar answered Oct 28 '22 23:10

TigerTiger


for WORDPRESS:

instead of:

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

should be:

if (!get_option('smth'))
like image 20
T.Todua Avatar answered Oct 28 '22 22:10

T.Todua


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.

like image 33
middus Avatar answered Oct 28 '22 23:10

middus


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.
like image 37
Mehdi Karamosly Avatar answered Oct 29 '22 00:10

Mehdi Karamosly


Another scenario where this error is trigered due syntax error:

ucwords($variable) = $string;
like image 24
tomelin5 Avatar answered Oct 29 '22 00:10

tomelin5


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

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

by

if (isset($_POST['sms_code'] == TRUE)
like image 44
Diego Avatar answered Oct 29 '22 00:10

Diego


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.

like image 28
husnixs Avatar answered Oct 28 '22 23:10

husnixs