Jump to content

[SOLVED] Another way to break an if statement?


Kane250

Recommended Posts

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?

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 />";
}
?>

Link to comment
Share on other sites

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 />";
    }
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.