7

the page code is all html and it wont load bec. of this error:

Fatal error: Can't use function return value in write context line 142

code:

<div>
    <div class="">
        <input type="text" id="select_shelves" name="select_shelves" class="shelves_select_and_buttons" />
        <div id="shelves_menu" >
            <ul>
                <li id="li_" onclick="printValue();">option5 <= line 142 </li>
            </ul>
        </div>
    </div>
    <div class="button">
        <input type="button" onclick="dropShelves();" id="Shelves_select_button" name="Shelves_select_button" value="" class="grey_button"/>
    </div>
</div>
hakre
  • 193,403
  • 52
  • 435
  • 836
bm2001
  • 105
  • 1
  • 1
  • 4

2 Answers2

32

If there is some PHP behind, the problem could be calling a function empty($var) in this way:

if(empty($var = getMyVar())) { ... }

Instead of this You should call it this way:

$var = getMyVar();
if(empty($var)) { ... }

Or better (as deceze has pointed out)

if(!getMyVar()) { ... }

Problem causes also other similar functions (isset, is_a, is_null, etc).

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • Again, this will not throw the *Can't use function return value in write context* (on 5.3, it'll throw a syntax error)... – ircmaxell Apr 18 '11 at 13:21
  • there is no php code just html – bm2001 Apr 18 '11 at 13:21
  • 1
    @bm2001: if there is only HTML code, what is throwing you that error??? and why did you tag the question PHP? – Trufa Apr 18 '11 at 13:31
  • http://stackoverflow.com/questions/1532693/weird-php-error-cant-use-function-return-value-in-write-context – AJJ Apr 18 '11 at 13:38
  • 6
    Just do `if (!getMyVar())`. No `empty` needed at all. – deceze Jun 18 '12 at 13:34
  • @deceze We are only supposing there is similar construct based on the error message from OP. Do not see nothing wrong with my answer, so why -1? – shadyyx Jun 18 '12 at 14:53
  • Because `$var = func(); if (empty($var))` is a misuse of `empty`, you should use `if (!func())` or `if (!$var)` or `if (($var = func()) == false)` instead. See [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/). – deceze Jun 18 '12 at 15:21
  • So tomorow I will write some guide after reading which You will give another users -1? Misuse of `empty`... Where did You find this 'construct' within the guide? – shadyyx Jun 18 '12 at 21:21
14

you are probably using something like:

if(empty(urFunc($foo)){
    ....
}

which won't work.

what ever it is (look for the lien number in the error to locate it) change it to:

$foo = urFunc($bar);
if(empty($foo)){
    ....
}

that should fix it.

Ian Wood
  • 6,515
  • 5
  • 34
  • 73
  • That will not generate that error. The error you'd get would be different (there would be an error though)... – ircmaxell Apr 18 '11 at 13:17
  • erm... I was fairly sure it that it will (http://forums.devshed.com/php-development-5/fatal-error-can-t-use-method-return-value-in-write-383832.html) – Ian Wood Apr 18 '11 at 13:22
  • I would suggest to reply to @ircmaxell like this since otherwise he won't get notified I think... – Trufa Apr 18 '11 at 13:27
  • @AnonymousLoozah: I don't agree, given answers without a good enough context can be harmful for future users, besides I would not recommend using the up-vote for trying rather I would use it for gave the correct answer. – Trufa Apr 18 '11 at 13:28
  • 1
    What you really want is `if (!urFunc($bar))`. No `empty` needed at all. – deceze Jun 18 '12 at 13:34
  • Why would this make for an error? Imagine urFunc() returns a string, why not use empty() to check whether the string is ""? – Kevin Van Ryckegem Mar 04 '16 at 17:51
  • 1
    @KevinVanRyckegem http://php.net/manual/en/function.empty.php - priorto 5.5 you could only use it on variables. – Ian Wood Mar 05 '16 at 12:05
  • @IanWood great! that's good to know! – Kevin Van Ryckegem Mar 05 '16 at 13:19