-1

Fatal error: Can't use function return value in write context in /home/gdcrpgin/public_html/manage_mypage.php on line 79

The complete web project was working fine until it was on my local windows machine since i uploaded this to online linux server (Hostgator) one of my page started showing this error I don't have any idea what this error mean i tried troubleshooting it at my level but nothing was achieved.

Line 79 is

<input type="text" name="home" class="form-control" maxlength="60" value = "<?php if(!empty(getuserfield($conn,"home"))) echo getuserfield($conn,"home") ?>"  required>

Function getuserfield() return nothing more than "Fake" string. I commented out its actual work to see it helps or not but no luck.

function getuserfield($conn,$field)
{
    /*
    $table = $_SESSION['type'];
    $key = $_SESSION['key'];
    $user = $_SESSION['username'];  
    $query = "select $field from $table where $key = '$user'";  
    $query_run = mysqli_query($conn,$query) or die(mysqli_error($conn));    
    while($fetch = mysqli_fetch_array($query_run)){
    if(!empty($fetch[0]))
        return $fetch[0];
    else
        return null;
    }*/
    return "Fake";
}
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Rishabh Gusain
  • 683
  • 1
  • 6
  • 23
  • Is this the only place where you are getting an/this error? – Glubus Feb 25 '16 at 15:50
  • yeah this is the only page where i am getting this error but if i comment out the line 79 then it gives same error on line 85 " required> but not in the first textbox that have no empty check " disabled> – Rishabh Gusain Feb 25 '16 at 15:54

2 Answers2

1

One google query about your problem got me the the solution to your problem (probably). If this is indeed the answer, you're probably guilty of not doing any research before coming here, shame.

You can find the solution here: Can't use method return value in write context

You're probably running an old version of php. This seems VERY likely since your application used to work, and now it doesn't on a "new" environment. The function empty() (so it's not about your getuserfield()) can only accept expressions since PHP 5.5.

So to fix your problem, install a newer version of PHP.

Community
  • 1
  • 1
Glubus
  • 2,819
  • 1
  • 12
  • 26
  • I don't think hostgator will allow me to intall a new version of PHP on their server and i really dont want to downgrade my site and change almost 3 KLOC I have used this techniques so many times in the web project I did search about the error but didn't find any clue about incompatibility – Rishabh Gusain Feb 25 '16 at 16:01
  • If you follow the link I've provided you'll see that you're doing the same the poster of the other question has asked. What version of PHP are they running? If you find that they're running an old version you should ask yourself why on earth you're hosting your application there. If you NEED to run it there and they don't want to install a new version, then you're forced to downgrade, but then again, if these were your constraints you obviously shouldn't have developed your application on a newer version. – Glubus Feb 25 '16 at 16:03
  • 1
    HostGator allows you to update the version: http://support.hostgator.com/articles/hosting-guide/hardware-software/what-version-of-php-are-you-using – aynber Feb 25 '16 at 16:08
  • Thanks dude for the information they are running PHP 5 actually this is my first web project that is so big for me previously I coded some small application using php 5.5 and had no problem hosting them on hostgator. Ur information saved my day.Now I will not have to go backward and code again with php 5.0 standards. – Rishabh Gusain Feb 25 '16 at 16:14
1

empty is used to test whether a variable doesn't exist or is falsey. You're using it on the return value of a function. Those are incompatible uses. Get rid of empty, you don't need it here. if (getuserfield(..)) does exactly the same thing for a function.

It happens to work on another server because since PHP 5.5, empty also accepts expressions of any sort. One of your servers has PHP 5.5, the other something older. Though empty is still used superfluously here.

You should always be aware of the PHP version you're using to develop, where you're going to deploy to, and that both should be identical or at least the same major release version.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Your first paragraph is sense less but others two totally makes a great sense which i totally agree with. You can see i called the function in the value property of a text box and it is returning value form db what if there is nothing in db for that specific field it will fill the textbox value property with php error in html formate. – Rishabh Gusain Feb 25 '16 at 16:20
  • "Senseless"?! Wow, uhm, thanks a bunch. You may want to read this: [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/) – deceze Feb 25 '16 at 16:21