Kane250 Posted May 28, 2008 Share Posted May 28, 2008 I just tried using code on a new server with php5 and my php4 code which uses break; in an elseif statement pulls an error message. This message: Fatal error: Cannot break/continue 1 level in... I guess that's the wrong way? Quote Link to comment Share on other sites More sharing options...
awpti Posted May 28, 2008 Share Posted May 28, 2008 Break is used for loops. if() is not a loop. You never need to break out of an if. It's called a conditional for a reason. Something happens or not based upon your check. A loop runs until you tell it to stop. Quote Link to comment Share on other sites More sharing options...
Kane250 Posted May 28, 2008 Author Share Posted May 28, 2008 Break is used for loops. if() is not a loop. You never need to break out of an if. It's called a conditional for a reason. Something happens or not based upon your check. A loop runs until you tell it to stop. Right, I realize that. However it actualy works in my code for what I need it to do, because I am testing on multiple things in the same if. Since this is a bad way of using it, is there a simple replacement that could tell the elseif to stop testing for the rest of the things below it if something occurs? Quote Link to comment Share on other sites More sharing options...
AndyB Posted May 28, 2008 Share Posted May 28, 2008 I predict you'll get a more useful response if you show us some of your 'bad way' code. Otherwise, this is going to be all guesswork. Quote Link to comment Share on other sites More sharing options...
Kane250 Posted May 28, 2008 Author Share Posted May 28, 2008 I predict you'll get a more useful response if you show us some of your 'bad way' code. Otherwise, this is going to be all guesswork. Sorry, I guess that would help huh? The break appears right before the else; <?php if (isset($_POST['submit'])) { if (!empty($_POST['email'])) { //HERE WE ARE DECLARING VARAIBLES TO SEE IF THE EMAIL ADDRESS IS ALREADY ENTERED INTO THE DATABASE $emailaddy = $_POST['email']; $doubleaddycheck = "SELECT * FROM emaillist WHERE email = '$emailaddy'"; $doubleaddycheck = mysql_query($doubleaddycheck); if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) { print "Please enter a valid e-mail address<br />";} //AND HERE WE ARE USING THOSE VARIABLES TO CHECK THE DATABASE elseif (mysql_num_rows($doubleaddycheck) > 0) { print "That e-mail address has already been added!<br />"; break;} else { $emailaddy2 = $_POST['email']; } } else { print "Please enter an e-mail address<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
Prismatic Posted May 28, 2008 Share Posted May 28, 2008 Like stated before, if is conditional, break is not required or even valid. <?php if (isset($_POST['submit'])) { if (!empty($_POST['email'])) { $emailaddy = $_POST['email']; $doubleaddycheck = "SELECT * FROM emaillist WHERE email = '{$emailaddy}'"; $doubleaddycheck = mysql_query($doubleaddycheck); if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) { print "Please enter a valid e-mail address<br />"; } elseif (mysql_num_rows($doubleaddycheck) > 0) { print "That e-mail address has already been added!<br />"; } else { $emailaddy2 = $_POST['email']; } } else { print "Please enter an e-mail address<br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
Kane250 Posted May 28, 2008 Author Share Posted May 28, 2008 The reason I wanted to use break was because when I remove it, it tends to print that line that follows it. It runs through that last else statement for some reason. I guess I could rework the statement maybe Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 28, 2008 Share Posted May 28, 2008 1) As stated, break has no bearing on an if-statement. 2) Even if it did, your break comes right before a closing curly bracket, which ends that block of an if-statement anyways. The reason I wanted to use break was because when I remove it, it tends to print that line that follows it. 3) Then something else is wrong and you have a serious misunderstanding of how logic works. <?php // Your code, simplified if ( A ) { if ( B ) { if ( C ) { } elseif ( D ) { print "That e-mail address has already been added!<br />"; break; } else { } } else { // Call this NOT-B, or !B print "Please enter an e-mail address<br />"; } } ?> You're saying that if you remove the break the Please enter an e-mail address line prints? How is that possible. break can only execute if B is true. The Please enter an e-mail address can only print if B is NOT true. So how can the break have any bearing on that? B can't be true and false at the same time. 4) In your original code, it appears you are missing a closing curly bracket, which Prismatic added when he cleaned it up. Quote Link to comment Share on other sites More sharing options...
Kane250 Posted May 28, 2008 Author Share Posted May 28, 2008 Yeah I'm an idiot, it was that curly bracket that was causing it to do that, not the break. Lets forget I ever asked this question! Haha thanks again for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.