Jump to content

[SOLVED] Another way to break an if statement?


Kane250

Recommended Posts

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?

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.