Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

Using break within a if statement ?

I know you can use break statements with switch statements, however is it acceptable to use break statements within if statements ?

Many thanks.
Avatar of cforant
cforant
Flag of United States of America image

Not so sure why you would need to do this, but I would venture to say that there is no need for this type of usage.  An IF statement is intended to be true or false.  I believe you should re-evaluate the way the code is written if you think you need to use a BREAK within an IF statement.

if your intentions are to stop the script entirely,I would use EXIT() from within this IF statement.  This is acceptable.

If I am off base here, someone let me know.
if ($something) {
do this.
exit;     <--- this is what you want
}
that will exit the entire script.
ASKER CERTIFIED SOLUTION
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Break is used in loops and switches, not conditional statements. Doing so results in a fatal error.