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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP & MySql - Turn Select Query Results into a Glyphicon 3 28
insert into database through form with dynamic fields. 2 28
hasing a url 16 24
PHP curl issue VERBOSE output 18 34
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now