correct use of break in 'if' loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

correct use of break in 'if' loop question

Post by seezee »

I have an if loop like this:

Code: Select all

if($zip!=""){
	if(preg_match('/^\d{5}(-\d{4})?$/',$zip)){
		break 2;
		} else {
		$errors=1;
		$error.="<li>Please enter a valid <abbr>us</abbr> ZIP code (5 digit ZIP or ZIP + 4)</li>";
	}
}
But when I input a valid (matching) string, I get an error: PHP Fatal error:
Cannot break/continue 1 level in path/to/file.php on line xyz
I've worked around this with

Code: Select all

if($zip!=""){
	if(preg_match('/^\d{5}(-\d{4})?$/',$zip)){
		$errors=$errors;
		} else {
		$errors=1;
		$error.="<li>Please enter a valid <abbr>us</abbr> ZIP code (5 digit ZIP or ZIP + 4)</li>";
	}
}
But I'd like to understand the correct way to do this.

Thanks,

--cz
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: correct use of break in 'if' loop question

Post by bradbury »

break ends execution of the current for, foreach, while, do-while or switch structure but does not end the loop for a if statement.
Hope that helps you out.
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: correct use of break in 'if' loop question

Post by bradbury »

btw the php manual should be your best friend. I pulled that straight from the manual after searching break.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: correct use of break in 'if' loop question

Post by AbraCadaver »

This is functionally the same and probably cleaner and more to the point:

Code: Select all

if(!empty($zip)){
        if(!preg_match('/^\d{5}(-\d{4})?$/', $zip)){
                $errors = 1;
                $error .= "<li>Please enter a valid <abbr>us</abbr> ZIP code (5 digit ZIP or ZIP + 4)</li>";
        }
}
And really, you can use $error as an array and then use count($errors) later to see how many:

Code: Select all

$error = array();
if(! empty($zip)){
        if(! preg_match('/^\d{5}(-\d{4})?$/', $zip)){
                $error[] = "<li>Please enter a valid <abbr>us</abbr> ZIP code (5 digit ZIP or ZIP + 4)</li>";
        }
}
$errors = count($error);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

Re: correct use of break in 'if' loop question

Post by seezee »

Thanks, Bradbury. I did RTFM, but that distinction escaped me as I was doing a lot of skimming. :-)
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

Re: correct use of break in 'if' loop question

Post by seezee »

Abracadaver,

Of course, you're right. I actually figured that out on my own after my last post.
Post Reply