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

Posted on 2004-09-15
Last Modified: 2008-03-17
 I'm having an error in PHP running on Apache running on windows.
The error message is:

PHP Fatal error:  Can't use function return value in write context ...... in filename on line number

What I believe is causing the error is that I am passing a value to a function then passing that same varible to another function, in Visual Basic I would fix this problem using a ByVal keyword to make sure that I am passing the value not the reference to the varible. Here is the code that causes the error:


$x = "01/01/2004";
$r[0] = "01/01/2004";

if( date_compare($x, $r[0]) )
      print("strange error because dates are equal");

function DatePart($interval, $date) {

    $date_time_array = getdate($date);
    $hours = $date_time_array['hours'];
    $minutes = $date_time_array['minutes'];
    $seconds = $date_time_array['seconds'];
    $month = $date_time_array['mon'];
    $week = $date_time_array['wday']+1;
    $day = $date_time_array['mday'];
    $year = $date_time_array['year'];

    switch ($interval) {
        case 'yyyy':
            return $year;
        case 'm':
            return $month;
        case 'y':
        case 'w':
          return $week;
        case 'd':
            return $day;
        case 'h':
            return $hours;
        case 'n':
            return $minutes;
        case 's':
            return $seconds;
    return -1;

function date_compare( $d1, $d2 )
      if( DatePart("yyyy", $d1) = DatePart("yyyy", $d2) )      //<--------------error occurs here
            if( DatePart("m", $d1) = DatePart("m", $d2) )
                  if( DatePart("w", $d1) = DatePart("w", $d2) )
                        if( DatePart("d", $d1) = DatePart("d", $d2) )
                              if( DatePart("d", $d1) = DatePart("h", $d2) )
                                    if( DatePart("d", $d1) = DatePart("n", $d2) )
                                          if( DatePart("d", $d1) = DatePart("s", $d2) )
                                                return 1;
      return 0;

In the function date_compare I have tried saving the varible $d1 into another varible and passsing that varible instead but that did not fix the error - got the same error message. The error message does not appear on the webpage, the web page never gets executed at all.
Any ideas?
Question by:funkyfinger
  • 2
LVL 13

Expert Comment

ID: 12069150
Am i completely insane?

Try this:

$x = strtotime("01/01/2004");
$r[0] = strtotime("01/01/2004");

if ($x == $r[0])
     print("not equal");

Why have those udfs?

Author Comment

ID: 12069472
I thought about this, it would be perfect, except that if hours exist for one and not the other it could become a problem.
"01/01/2004 12:34:00 AM" <> "01/01/2004".

Of course since in the function above I am checking for hours minutes and seconds then your answer is correct. I also noticed a few bugs in my code I am comparaing days to hours, minutes, and  seconds.

I could work around the time problem by creating a new varible that does not store time.

Thanks for the second set of eyes it's a big help
LVL 13

Accepted Solution

cLFlaVA earned 50 total points
ID: 12069648
Well, if I understand your problem correctly, then my solution would work.

if (strtotime("01/01/2004") == strtotime("01/01/2004"))
    echo 'equal';
    echo 'not equal<br>';

if (strtotime("01/01/2004 12:25:03") == strtotime("01/01/2004 10:15:07"))
    echo 'equal';
    echo 'not equal';

if (strtotime("01/01/2004 12:25:03") == strtotime("01/01/2004 12:25:03"))
    echo 'equal';
    echo 'not equal';

Would produce the following:

not equal

Since strtotime returns an integer timestamp that includes hours, minutes, and seconds.

Expert Comment

ID: 12079744
Good Day:

I'm unfamiliar with the error message you are recieving, but one thing I noticed when I was looking over your code flow is that in your date_compare function your using an assignment operator instead of a comparison one.  

if( DatePart("yyyy", $d1) = DatePart("yyyy", $d2) )     //<--------------assignment operator

Should be:

if( DatePart("yyyy", $d1) ==  DatePart("yyyy", $d2) )     //<--------------comparison operator

You can review the differences by taking a look at the PHP manual:

I'm sure this is probably just an oversight, I do it all the time when I'm tired.  Good luck!  

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question