php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23359 Passing by reference oddity
Submitted: 2003-04-26 06:06 UTC Modified: 2003-04-28 09:32 UTC
From: chen at hivemail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4CVS-2003-04-26 (stable) OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chen at hivemail dot com
New email:
PHP Version: OS:

 

 [2003-04-26 06:06 UTC] chen at hivemail dot com
Ok... I'll try my best to explain the problem. I have the following function:

function fixdate(&$month, &$year) {
	if ($month < 1) {
		$month = 12;
		$year--;
	} elseif ($month > 12) {
		$month = 1;
		$year++;
	}
}

So far so good, the function itself works ok. But when I try to do the following:

	fixdate($prevmonth = ($month - 1), $prevyear = $year);

I get this error:

	Fatal error: Only variables can be passed by reference in C:\foo.php on line 181

However, this code works as expected:

	fixdate($prevmonth = ($month - 1), $prevyear = ($year + 0));

You'd expect the first code ($prevyear = $year) to work as well, wouldn't you? The reason I use $prevyear = $year and not just pass $year is because I need to keep the value of $year after the function has executed.

This might be related to the way PHP copies variables, I'm not sure. If you need any more information, let me know. :)

Thanks,
Chen

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 12:01:36 2025 UTC