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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 20:01:34 2024 UTC