Re: Problem with Arrays?

From: Date: Sat, 22 Feb 2003 13:05:37 +0000
Subject: Re: Problem with Arrays?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-136619@lists.php.net to get a copy of this message
At 13:46 22.02.2003, Patrick Teague said: --------------------[snip]-------------------- >here's what I had that didn't print anything other than 3 blank lines for >this section of code - > >$byteSize[0] = "bytes"; >$byteSize[1] = "kb"; >$byteSize[2] = "mb"; > >function getMaxSize( $maxSize ) >{ > echo $byteSize[0] . "<br/>\n"; > echo $byteSize[1] . "<br/>\n"; > echo $byteSize[2] . "<br/>\n"; > .... >} --------------------[snip]-------------------- if you declare $bytesize global within the function it will work: function getMaxSize( $maxSize ) { global $bytesize; echo $byteSize[0] . "<br/>\n"; } You'd be still better off passing the array as variable: function getMaxSize( $bytesize, $maxSize ) { echo $byteSize[0] . "<br/>\n"; } And lastly, for performance issues, pass it as a reference: function getMaxSize( &$bytesize, $maxSize ) { echo $byteSize[0] . "<br/>\n"; } Without a reference, the array is copied to the function. By passing a reference the function is working on the original array, no copy overhead. Note: When passed as reference, any modification on the array within the function will effect the original array (same is true if declared global). Without reference the original array remains unchanged. -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/

« previous php.general (#136619) next »