不使用 goto 将变量转移到较早的点
如何在不使用 goto 的情况下编写此内容:
ob_start();
$a = 0;
echo "START of LEFT<br />";
begin:
if($a > 0) {
echo "CONTENT LEFT: $a<BR />";
<VERY DIFFICULT ALGORHITM>
goto end;
}
<... ALL THE REST CODE OF LEFT ...>
echo "END of LEFT<br /><br />";
$output1 = ob_get_contents();
ob_end_clean();
ob_start();
echo "START of CENTER<br />";
$a = 5; goto begin;
end:
<... ALL THE REST CODE OF CENTER ...>
echo "END of CENTER<br />";
$output2 = ob_get_contents();
ob_end_clean();
// print it
echo $output1.$output2;
要获得此 echo:
START of LEFT
CONTENT LEFT: 5
END of LEFT
START of CENTER
END of CENTER
要求:
1. 我不允许更改顺序(CORE( echo $a )和 PLUGIN( $a=5 )):
ob_start();
$a = 0;
<ANY CODE>
echo $a;
$output1 = ob_get_contents();
ob_end_clean();
ob_start();
<ANY CODE>
$a = rand(0,10);
$output2 = ob_get_contents();
ob_end_clean();
2.输出必须通过ob_get_contents()生成;
但我可以在某些地方编写任何代码。
// 解决方案 ob_get_contents(); 仅当想要替换输出 HTML 代码中的几行,但无法更改变量的值时才有帮助,以更改 ALGORHYTM(取决于 var 值) ),它生成随机 HTML 代码。
还, 当我检查我的代码时,我明白,我的代码即使使用 GOTO labels 语句,不会更改 $output1 内容?怎么做呢?唯一的方法是从头开始重新缓存 $output1 。或者也许我可以通过其他方式做到这一点?
How to write this without goto:
ob_start();
$a = 0;
echo "START of LEFT<br />";
begin:
if($a > 0) {
echo "CONTENT LEFT: $a<BR />";
<VERY DIFFICULT ALGORHITM>
goto end;
}
<... ALL THE REST CODE OF LEFT ...>
echo "END of LEFT<br /><br />";
$output1 = ob_get_contents();
ob_end_clean();
ob_start();
echo "START of CENTER<br />";
$a = 5; goto begin;
end:
<... ALL THE REST CODE OF CENTER ...>
echo "END of CENTER<br />";
$output2 = ob_get_contents();
ob_end_clean();
// print it
echo $output1.$output2;
To get this echo:
START of LEFT
CONTENT LEFT: 5
END of LEFT
START of CENTER
END of CENTER
Requirements:
1. I'm not allowed to change the order(CORE( echo $a ), and PLUGIN( $a=5 )):
ob_start();
$a = 0;
<ANY CODE>
echo $a;
$output1 = ob_get_contents();
ob_end_clean();
ob_start();
<ANY CODE>
$a = rand(0,10);
$output2 = ob_get_contents();
ob_end_clean();
2. Output must be generated via ob_get_contents();
But I'm allowed to write ANY CODE in places.
// Solvings
ob_get_contents(); Helps only if want to replace few lines in output HTML CODE, but can't change a value of variable, to change the ALGORHYTM(depends of var value), which generates the random HTML code.
Also,
As I checked my code, I understand, that my code, even with GOTO labels statement , DOES NOT going to change the $output1 content ?. How to do that? Is the only way is to recache the $output1 from his beggining. Or maybe I'm able to do this in other ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您熟悉方法/函数的概念吗?如果没有(而且似乎很有可能......),你真的应该首先了解一些有关这些的知识。然后,将单个代码块中的功能拆分为小的、可维护的代码片段就轻而易举了。
You are familiar with the concept of methods/functions? If not ( and it seems that chances are.. ) you should really learn something about those first. It's then a piece of cake to split functionality out of a monolithic block of code to small, maintainable pieces of code.
结构化编程 - http://php.net/manual/en/language.oop5.php
Structured programming - http://php.net/manual/en/language.oop5.php
如果我很好地理解你想要做什么,我会使用递归函数而不是 goto。例如,看看我前一段时间用 C 编写的数学表达式解析器:
主要思想是将代码拆分为函数,并且您可以说:
您将拥有:
这是长话短说。
If i understood well what you want to do I would use recursive functions instead of goto. for example look at this math expression parser i've witten in C some time ago:
The main idea is to split the code in functions and where you have let's say:
you'll have:
That's the long story said short.