Solved

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

Posted on 2004-09-15
6
9,416 Views
Last Modified: 2008-03-17
Hello,
 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("working");
else
      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;
            break;
        case 'm':
            return $month;
            break;
        case 'y':
        case 'w':
          return $week;
          break;
        case 'd':
            return $day;
            break;
        case 'h':
            return $hours;
            break;
        case 'n':
            return $minutes;
            break;
        case 's':
            return $seconds;
            break;            
    }
    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?
thanks
ff
0
Comment
Question by:funkyfinger
  • 2
6 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
Comment Utility
Am i completely insane?

Try this:

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

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


Why have those udfs?
0
 
LVL 2

Author Comment

by:funkyfinger
Comment Utility
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
ff
0
 
LVL 13

Accepted Solution

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

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

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

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

Would produce the following:

equal
not equal
equal

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

Expert Comment

by:nowaydown1
Comment Utility
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:
http://us4.php.net/language.operators

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now