182

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);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
cfischer
  • 24,452
  • 37
  • 131
  • 214

12 Answers12

497

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.

Fabian
  • 3,465
  • 4
  • 34
  • 42
rolfen
  • 4,971
  • 2
  • 15
  • 2
  • Dang, I just got this error too, doing your exact example (trim inside empty). Thanks x3. What a strange error.. I still don't fully understand :/ empty() can take a string.. and trim() returns a string.. so wth? – Nick Rolando Feb 08 '12 at 17:23
  • 15
    @Shredder - It's because `empty()` isn't actually a function; it's a language construct, like `echo`. The PHP parser handles language constructs differently. In the case of `empty`, it looks at the parameter as a variable, not something to be evaluated, so if you try to pass a function, it breaks. At least that's my understanding of it. More info [here](http://www.phpknowhow.com/basics/language-constructs-vs-built-in-functions/) and [here](http://www.php.net/manual/en/function.empty.php). – grant Jun 26 '12 at 21:04
  • 1
    This is the exact error I ran into as well. Why isn't this the top answer!? – Adam Fowler Feb 22 '13 at 17:04
  • make this top answer or add a reference here on the accepted answer. – Sharky Apr 04 '13 at 07:30
  • 2
    My colleague was getting this error while I was not. Confusion ensued, until I checked the docs - as of PHP 5.5, empty() will accept the return from a function as well as a variable. http://us3.php.net/empty – James Alday Feb 26 '14 at 19:30
  • 1
    Horray for 5.5! Until you upgrade, you could throw something like this into your main function include file: `function mTEE($val){ return empty($val); }` – TecBrat Mar 14 '14 at 15:18
  • Like @JamesAlday said, from documentation `Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.`. – PhoneixS Nov 05 '14 at 08:53
  • Thanks a lot! I upgraded to PHP 5.5 and it solved my problem. – William Gérald Blondel Aug 04 '15 at 00:24
  • Thank you! very important thing to know! :) Although this error doesn't show up in higher versions than 5.4 i think! but still its very good point to be noted. – BlackBurn027 Aug 27 '16 at 06:26
  • I know it's late but thanks, this was my problem, appreciate the fix :). I was struggling with it since it worked locally but not on our server. – Joshua Bakker Sep 19 '17 at 12:55
  • I know this is an old issue, but I encountered the same thing and it was the inline trim() causing the problem on 5.4.x. – seanbreeden Jun 22 '18 at 15:58
113

You mean

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

though incidentally you really mean

if (isset($_POST['sms_code'])) {
chaos
  • 122,029
  • 33
  • 303
  • 309
22
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 
GAMITG
  • 3,810
  • 7
  • 32
  • 51
TigerTiger
  • 10,590
  • 15
  • 57
  • 72
  • 1
    Nope, you cannot write "if (isset($_POST['sms_code'] == TRUE ) {", there's a missing ")". – middus Oct 07 '09 at 16:26
  • 1
    +several billion cool points for the phrae 'you are using parenthesis...but you wanted square brackets', which was what my problem (that led me to this question) was – Kevin Horn Mar 22 '12 at 14:36
  • 1
    Phew, thx man! Now, if the parser would say "I expected square brackets after an array variable's name, duh!", it would sound much cooler than "Can't use function return value in write context." I might submit that to the PHP guys as a suggestion. – Joe Völker Dec 25 '12 at 09:40
13

for WORDPRESS:

instead of:

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

should be:

if (!get_option('smth'))
T.Todua
  • 53,146
  • 19
  • 236
  • 237
11

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.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
middus
  • 9,103
  • 1
  • 31
  • 33
5

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.
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
  • Do you want to get bugs? Because this is how you get bugs. No but seriously, don't ever do this. Its like doing `if(i++)`, it may look shorter than incrementing the counter and then evaluating its value, but it is going to cause you headaches if it ever bugs out. Edit: Im not implying that you do it, just pointing it out to new programmers reading the answer and thinking this is a neat thing, to increment values while calculating or evaluating that which is being incremented. – Victor D. Oct 01 '14 at 20:56
  • @VictorD. I am just saying that error could happen in that situation that means it is not good to do it. – Mehdi Karamosly Oct 02 '14 at 23:56
  • Can I suggest one more? You do `=` instead of `==` in an if statement. – Josiah Mar 09 '16 at 18:19
  • For me it was PHP version, upgrade to 7.0 :) – nodws Apr 02 '18 at 16:24
  • still happening on PHP 8.1 `intval($strCounter)++` – jesusduarte Feb 12 '22 at 03:11
3

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

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

by

if (isset($_POST['sms_code'] == TRUE)
Warren Sergent
  • 2,542
  • 4
  • 36
  • 42
Diego
  • 41
  • 1
3

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.

husnixs
  • 37
  • 3
1

Another scenario where this error is trigered due syntax error:

ucwords($variable) = $string;
tomelin5
  • 55
  • 7
  • 1
    Is because `ucwords` return a string and the context of calling is incorrect, if you try for example with `$Test = ''; ${ucwords('test')} = 'String new !'; echo $Test;`, then show you the new value assigned. – kip Sep 16 '17 at 18:17
0

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;
    }
Shaakir
  • 464
  • 5
  • 13
0

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.

-2

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

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136