有没有办法在PHP中中断if语句?
PHP中是否有停止执行当前或父级有没有办法在PHP中中断if语句?,php,Php,PHP中是否有停止执行当前或父级if语句的命令,与break或break(1)forswitch/loop相同。比如说 $arr=array('a','b'); foreach($arr as $val) { break; echo "test"; } echo "finish"; 在上面的代码中,PHP不会执行echo“test”并将转到回显“完成” 我需要这个 $a="test"; if("test"==$a) { break; echo "yes"; // I don'
if
语句的命令,与break
或break(1)
forswitch
/loop
相同。比如说
$arr=array('a','b');
foreach($arr as $val)
{
break;
echo "test";
}
echo "finish";
在上面的代码中,PHP不会执行echo“test”代码>并将转到回显“完成”代码>
我需要这个
$a="test";
if("test"==$a)
{
break;
echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
我想中断上面的if
语句,并停止执行回显“yes”
或不再需要执行的代码,可能存在或可能不存在附加条件,有没有办法做到这一点
更新:在发布这个问题仅仅两年后,我长大了,我学会了如何将代码编写成小块,为什么嵌套if会成为一种代码味道,以及如何首先通过编写可管理的小函数来避免此类问题。否
但如何:
$a="test";
if("test"==$a)
{
if ($someOtherCondition)
{
echo "yes";
}
}
echo "finish";
:
goto运算符可用于跳转到程序中的另一个部分。目标点由一个标签后跟一个冒号指定,并且给出的指令是goto后跟所需的目标标签。这不是一个完全不受限制的转到。目标标签必须位于同一文件和上下文中,这意味着您不能跳出函数或方法,也不能跳入其中。您也不能跳入任何类型的循环或开关结构。你可以跳出这些,一个常见的用法是用一个goto来代替多级中断
将代码封装在函数中。您可以随时停止执行带有return
的函数。只需将不应执行的代码移动到else/elseif
分支即可。我真的不明白你为什么要做你想做的事。不要担心其他用户的评论,我可以理解你,有时候,在开发这种“花式”的东西时,需要做一些事情。如果我们能够打破If,那么就不需要很多嵌套的If,从而使代码更干净、更美观
$a="test";
if("test"!=$a)
{
echo "yes";
}
else
{
echo "finish";
}
此示例代码说明了某些情况下,中断if可能比许多丑陋的嵌套if更合适。。。如果你没有面对这种情况并不意味着它不存在
丑陋代码
if(process_x()) {
/* do a lot of other things */
if(process_y()) {
/* do a lot of other things */
if(process_z()) {
/* do a lot of other things */
/* SUCCESS */
}
else {
clean_all_processes();
}
}
else {
clean_all_processes();
}
}
else {
clean_all_processes();
}
do {
if( !process_x() )
{ clean_all_processes(); break; }
/* do a lot of other things */
if( !process_y() )
{ clean_all_processes(); break; }
/* do a lot of other things */
if( !process_z() )
{ clean_all_processes(); break; }
/* do a lot of other things */
/* SUCCESS */
} while (0);
好看的代码
if(process_x()) {
/* do a lot of other things */
if(process_y()) {
/* do a lot of other things */
if(process_z()) {
/* do a lot of other things */
/* SUCCESS */
}
else {
clean_all_processes();
}
}
else {
clean_all_processes();
}
}
else {
clean_all_processes();
}
do {
if( !process_x() )
{ clean_all_processes(); break; }
/* do a lot of other things */
if( !process_y() )
{ clean_all_processes(); break; }
/* do a lot of other things */
if( !process_z() )
{ clean_all_processes(); break; }
/* do a lot of other things */
/* SUCCESS */
} while (0);
正如@NiematojakTomasz所说,使用goto
是一种替代方法,其缺点是您始终需要定义标签(点目标)。正确的方法:
try{
if( !process_x() ){
throw new Exception('process_x failed');
}
/* do a lot of other things */
if( !process_y() ){
throw new Exception('process_y failed');
}
/* do a lot of other things */
if( !process_z() ){
throw new Exception('process_z failed');
}
/* do a lot of other things */
/* SUCCESS */
}catch(Exception $ex){
clean_all_processes();
}
在阅读了一些评论之后,我意识到异常处理对于正常的流控制并不总是有意义的。对于正常控制流,最好使用“If else”:
您还可以将进程返回值保存在变量中,然后检入进程失败的失败/异常块。否,无法像在循环中那样“中断”if块。:(
所以,把你的测试变成一个开关
我想知道为什么没有人鼓励您使用它(即使您没有使用过很多测试用例)
你觉得它太冗长了吗
我在这里一定会去的
switch($a){
case 'test':
# do stuff here ...
if(/* Reason why you may break */){
break; # this will prevent executing "echo 'yes';" statement
}
echo 'yes'; # ...
break; # As one may already know, we might always have to break at the end of case to prevent executing following cases instructions.
# default:
# something else here ..
# break;
}
对我来说,异常是为了引起错误,而不是真正控制执行缺陷。
如果您试图设置的中断行为与意外错误无关,则异常处理不是正确的解决方案
$a = 1;
switch($a) {
case "1":
if ($condition1){
break;
}
if ($condition2){
break;
}
if ($condition3){
break;
}
}
通过这种方式,我得到了我想要的。我使用一个开关只有一个确定的大小写,然后使用断开的大小写来选择if条件。我使用断开的原因:条件1和条件2可能都满足,在这种情况下,只有条件1被应用。if根据顺序是选择性的。简单的解决方法是注释掉它
$a="test";
if("test"==$a)
{
//echo "yes"; //no longer needed - 7/7/2014 - updateded bla bla to do foo
}
附加的好处是您不需要更改原始代码,您可以为其添加日期、首字母并说明原因
为什么要投反对票,根据OP的要求,我认为这是一个完全有效的解决方案
“我想[中断上面的if语句并]停止执行echo“yes”;或者不再需要执行的代码,可能存在或可能不存在附加条件,有办法做到这一点吗?”
事实上,有人可能会在一年后看到其他一些解决方案,并想知道那里发生了什么。根据我的建议,可以留下好的文档供将来参考,这始终是一种好的做法。您可以使用do while(错误):
如中所述,简单的答案是不,如果不完全停止执行(通过退出
),就无法从if
语句中断。其他解决方案对我不起作用,因为我无法更改if
语句的结构,因为我正在向插件中注入代码,如下所示:
if(条件){
//我想使用的代码和变量
//我能控制的代码
//我不想运行的代码
}
//更多我想使用的代码
回答您的问题,这是否可以实现,然后是可以使用php的“goto”操作符实现的
但从伦理上讲,使用“goto”并不是一个好的实践,如果需要使用goto,那么这意味着需要重新构建代码,以便删除goto的要求
根据您在上面发布的示例代码,可以清楚地看到代码可以重建,不再需要的代码可以删除或注释(如果将来有可能使用)。使用三元运算符如何
<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
?>
与此if/else语句相同:
<?php
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}
?>
结合你的陈述,我认为这会给你带来你想要的结果。
简洁明了,没有太多的陈述
对于丑陋而好看的代码,我的建议是:
function myfunction(){
if( !process_x() || !process_y() || !process_z()) {
clean_all_processes();
return;
}
/*do all the stuff you need to do*/
}
在你正常代码的某个地方
myfunction();
我有一个简单的解决方案,没有太多的变化。
最初的声明是
myfunction();
$a="test";
if("test"==$a)
{
if (1==0){
echo "yes"; // this line while never be executed.
// and can be reexecuted simply by changing if (1==0) to if (1==1)
}
}
echo "finish";
$a="test";
echo "finish";
do
{
$subjectText = trim(filter_input(INPUT_POST, 'subject'));
if(!$subjectText)
{
$smallInfo = 'Please give a subject.';
break;
}
$messageText = trim(filter_input(INPUT_POST, 'message'));
if(!$messageText)
{
$smallInfo = 'Please supply a message.';
break;
}
} while(false);
if(smth) {
.....
.....
.....
.....
.....
goto Area1;
.....
.....
}
Area1:
....your code here....
$a ='';
$b ='';
if($a == $b){
echo 'Clark Kent is Superman';
exit();
echo 'Clark Kent was never Superman';
}
Clark Kent is Superman
foreach($arr as $val)
{
exit();
echo "test";
}
echo "finish";
nothing gets printed here.
for ($x = 2; $x < 12; $x++) {
echo "Gru has $x minions <br>";
if($x == 4){
exit();
}
}
Gru has 2 minions
Gru has 3 minions
Gru has 4 minions
$a ='Make hot chocolate great again!';
echo $a;
exit();
$b = 'I eat chocolate and make Charlie at the Factory pay for it.';
Make hot chocolate great again!