?
Solved

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

Posted on 2004-09-15
6
Medium Priority
?
9,458 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
4 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12069150
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
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
ff
0
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 200 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';
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
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:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 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