Is there any command in PHP to stop executing the current or parent if
statement, same as break
or break(1)
for switch
/loop
. For example
$arr=array('a','b');
foreach($arr as $val)
{
break;
echo "test";
}
echo "finish";
in the above code PHP will not do echo "test";
and will go to echo "finish";
I need this for if
$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";
I want to break
the if
statement above and stop executing echo "yes";
or such code which is no longer necessary to be executed, there may be or may not be an additional condition, is there way to do this?
Update: Just 2 years after posting this question, I grew up, I learnt how code can be written in small chunks, why nested if's can be a code smell and how to avoid such problems in the first place by writing manageable, small functions.
echo
(in your example) will never be executed. So you might as well delete it.try catch
an option?if (condition) { if ( oneothercondition ) stop; if ( yetothercondition ) stop; // go ahead , all is fine }
I don't want this line or lines after to be executed, without using another if
... then don't type the line of code. This minimal reproducible example makes no sense.