Solved

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

Posted on 2004-09-15
6
9,418 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
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 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';
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Permalink issue in Wordpress archive 1 32
PHP7 and Sql Server Windows 2008 R2 13 85
PHP warning 4 28
Bootstrap collapse causes odd behavior with php loop 7 20
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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 …
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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