Jump to content

Why is the output different for this script?


bmb

Recommended Posts

Very simple script produces different output when executed in a terminal and a browser. I've been banging my head against the wall trying to figure this out... Maybe someone smarter than I can..

 

This script is very simple and does the following:

  • Prints the Date and the Week Before Date
  • Makes the Week Before Current and Gets the Week Before That

 

Here's the Code:

<?php

class metricInfo {
	var $weekEnd;
}

// setup today
$epoc = gettimeofday();
$today = date('Y-m-d', $epoc['sec']);

// print html header
print "<html>\n";
print "<head>\n";
print"<title>Macallan Bug Metrics v1.0</title>\n";	
print "</head>\n";
print "<body>\n";

// just confirm the time...
print "Today is: $today<br>\n";

// get two new instances
$thisWeek = new metricInfo;
$prevWeek = new metricInfo;

// setup today
$thisWeek->weekEnd = $today;

for ($i= 0; $i < 4; $i++){

	// tell me what last week was
	$date = $epoc['sec'] - (86400 * (7 * ($i+1)));
	$prevWeek->weekEnd = date('Y-m-d', $date);
	print("debug: i = $i thisWeek: $thisWeek->weekEnd prevWeek: $prevWeek->weekEnd<br>\n");

	// last week is now the current week... 
	$thisWeek = $prevWeek;
	print("debug: i = $i thisWeek: $thisWeek->weekEnd prevWeek: $prevWeek->weekEnd<br><br>\n");
}

print "</body>\n";
print "</html>\n";

?>

 

The output from the Terminal is:

bmb-macbook:~/Projects/Metrics-Stage brianboerner$ php confuse.php

Today is: 2008-02-29<br>

debug: i = 0 thisWeek: 2008-02-29 prevWeek: 2008-02-22

debug: i = 0 thisWeek: 2008-02-22 prevWeek: 2008-02-22

 

debug: i = 1 thisWeek: 2008-02-22 prevWeek: 2008-02-15

debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15

 

debug: i = 2 thisWeek: 2008-02-15 prevWeek: 2008-02-08

debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08

 

debug: i = 3 thisWeek: 2008-02-08 prevWeek: 2008-02-01

debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01

 

The output from a web browser is:

Today is: 2008-02-29

debug: i = 0 thisWeek: 2008-02-29 prevWeek: 2008-02-22

debug: i = 0 thisWeek: 2008-02-22 prevWeek: 2008-02-22

 

debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15

debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15

 

debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08

debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08

 

debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01

debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01

 

 

Notice how after the first time through the loop the browser skips a week and the dates never differ; compared to the terminal that works as it's suppose to... However... here's an interesting tid bit.

 

If I change this line:

$thisWeek = $prevWeek;

to

$thisWeek = &$prevWeek;

 

THEN, the terminal matches the web browser, as I would expect. So, I'm trying to figure out how to get the first example to work. It's like the browser is interpreting the copy as an address assignment.

 

Any Thoughts?

bmb

 

Link to comment
Share on other sites

But I want the browser to be the same as the terminal... Sorry. I'm pretty new. I've only been writing php code for about a week now...

 

It still doesn't explain what *causes* them to be different. Are you saying I'm calling a different version from one place than the other?

Link to comment
Share on other sites

OK.. You were right.. but can someone explain to me why php4 and php5 treat the code differently.. and how do I get php5 to treat it like php4 does (which is the desired output).

 

localhost:~/Projects/Metrics-Stage brianboerner$ which php

/usr/bin/php

localhost:~/Projects/Metrics-Stage brianboerner$ php confuse.php

Today is: 2008-02-29<br>

debug: i = 0 thisWeek: 2008-02-29 prevWeek: 2008-02-22<br>

debug: i = 0 thisWeek: 2008-02-22 prevWeek: 2008-02-22<br><br>

debug: i = 1 thisWeek: 2008-02-22 prevWeek: 2008-02-15<br>

debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15<br><br>

debug: i = 2 thisWeek: 2008-02-15 prevWeek: 2008-02-08<br>

debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08<br><br>

debug: i = 3 thisWeek: 2008-02-08 prevWeek: 2008-02-01<br>

debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01<br><br>

 

localhost:~/Projects/Metrics-Stage brianboerner$ /usr/local/php5/bin/php confuse.php

Today is: 2008-02-29<br>

debug: i = 0 thisWeek: 2008-02-29 prevWeek: 2008-02-22<br>

debug: i = 0 thisWeek: 2008-02-22 prevWeek: 2008-02-22<br><br>

debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15<br>

debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15<br><br>

debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08<br>

debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08<br><br>

debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01<br>

debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01<br><br>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.