|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-05-20 18:07 UTC] dead-krolik at gmail dot com
Description:
------------
It's not a bug, it's a new language operator suggestion. When I use other languages and PHP it's often were a one task - return values from function. I suggest a new operator and a don't know any analogs in other languages (may be I now small count of languages).
Reproduce code:
---------------
Task - return values from function with accumulating of results. For example, when we read strings from file and want return array of this strings (may be modified) from function we must accumulate/collect all values in array or in string:
function read_array($in) {
$out = array();
foreach(file($in) as $str) {
$str = trim($str);
//some modifications
$out[] = $str;
//OR $all_str .= $str
}
return $out;
//OR return $all_str
}
It's a very often task. And may be it's will be useful for programers have two new operators - for strings and for arrays, that accumulate values within all function body and at the end return ALL strings/elements automatically.
For example:
function read_array($in) {
foreach(file($in) as $str) {
$str = trim($str);
//some actions
return_array $str;//accumulate $str to internal temporary array, that will be returned at the end of function
}
}
And "return_string" for strings temporary buffer.
--
Sorry for my terible English. With best regards Dead Krolik.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 23:00:02 2025 UTC |
Howdy you can use a reference parameter to accumulate values like this. function read_array($in, &$accumulator) { $out = array(); foreach(file($in) as $str) { $str = trim($str); //some modifications $out[] = $str; //OR $all_str .= $str $accumulator[] = $str; // or $accumulator .= $str; } return $out; } Now after you call the function the last parameter will hold all of the new values, this is a nasty way though and I would suggest you read some more about references(http://au2.php.net/manual/en/language.references.php). You can ofcourse use a global variable to store this aswell if it was an OO based thing (don`t use global keyword if your not using OO dear god please!). +1 for marking this as bogus, bad programming practice should not a language make :)