The question is on the verge of a foul: how to declare a variable in php that should live until the end of the cycle?
You can always assign false at the beginning of the loop or delete the variable at the end of the loop.
But for me both options are not interesting and you’ll not really follow up with auxiliary variables.
In addition to the variant, the internal logic of the loop is taken out into a separate function are there options?
|
1 answer
it seems like no.
although there is another option, can you combine all the necessary variables into an array? set the array structure before the loop, assign this structure to the temporary array in the inner loop ...
$arrTpl = array('var1'=>'val1', 'var2'=>''); for (.....) { $tmpArr = $arrTpl; $tmpArr['val1'] = 'newval1'; $tmpArr['val2'] = 'newval2'; // чота делаем unset($tmpArr); // необязательно }
- you can set the structure of the array of course in the body of the loop, so as not to store it in memory, but for me it is better to be separate so as not to look for later where it is my personal IMHO;) - thunder
- Plus for thinking. But here a problem arises because of a bad programming style: when, as the code is being written, a bunch of side variables appear, or in a loop there are several if or case branches, each of which has its own variables. Since they were not removed at the end of the cycle, logic has suffered in places. Not to mention the fact that in the x-debugere such a garbage that his eyes hurt. - zenith
- Well, here it is - either try to get rid of a heap of variables (or reduce it somehow) or suffer the correct solution is hard to offer here. Perhaps you can search for another solution to the problem. and so, at least in the array, they will be in one place :) - thunder
- Or find the previous proger and tear off the hands :) By the way, "my IMHO" is a tautology, because [IMHO] [1] by definition, "mine." [1]: en.wikipedia.org/wiki/%D0%98%D0%BC%D1%85%B0 - zenith
- about the proger - yes. zyzh my-my opinion - thunder
|