note 97387 added to language.variables.scope

From: Date: Sat, 17 Apr 2010 00:14:23 +0000
Subject: note 97387 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-168338@lists.php.net to get a copy of this message
When functions call themselves, static variables aren't always the best option, while it will work for function test() { static $count = 0; $count++; echo $count; if ($count < 10) { test(); } $count--; } It wont always work for ones which call themselves in a loop (I don't have a simple example to show), but instead of counting recursions, it will simply count how many times the function is called, I think a much better way is as such: function test($count=0) { $count++; echo $count; if ($count < 10) { test($count); } } This still does the function of counting to 10 from 1 each time the function is called from outside itself. ---- Server IP: 69.147.83.197 Probable Submitter: 122.106.16.105 ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- https://master.php.net/note/edit/97387 Del: integrated -- https://master.php.net/note/delete/97387/integrated Del: useless -- https://master.php.net/note/delete/97387/useless Del: bad code -- https://master.php.net/note/delete/97387/bad+code Del: spam -- https://master.php.net/note/delete/97387/spam Del: non-english -- https://master.php.net/note/delete/97387/non-english Del: in docs -- https://master.php.net/note/delete/97387/in+docs Del: other reasons-- https://master.php.net/note/delete/97387 Reject -- https://master.php.net/note/reject/97387 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168338) next »