note 112643 deleted from functions.arguments by cmb

From: Date: Sun, 10 Nov 2019 13:18:25 +0000
Subject: note 112643 deleted from functions.arguments by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213023@lists.php.net to get a copy of this message
Note Submitter: ravenswd at gmail dot com ---- Be careful when passing arguments by reference, it can cause unexpected side-effects if one is not careful. I had a program designed to sweep through directories and subdirectories and report on the total number of files, and the total size of all files. Since it needed to return two values, I used variables passed by reference. In one spot in the program, I didn't need the values of those variables after they were returned, so I just used a garbage variable named $ignore instead. This caused a curious bug which took me a while to track down, because the effects of the bug were in a different part of the program than the place where I had made a mistake. Since the same variable was used for both parameters passed by reference, they ended up both pointing to the same physical location in memory, so changing one of them caused both of them to change. The code below is an excerpt of my program, stripped down to just the few lines necessary to illustrate what was happening: <?php sweep ($ignore, $ignore); // no errors occur here function sweep ( &$filecount, &$bytecount ) { $filecount = 1; $bytecount = 1024; print "Files: $filecount - Size: $bytecount"; // prints "Files: 1024 - Size: 1024" } ?>

« previous php.notes (#213023) next »