php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69853 break behavior unspecified when no argument is specified
Submitted: 2015-06-16 18:12 UTC Modified: 2015-06-27 22:34 UTC
From: chealer at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: chealer at gmail dot com
New email:
PHP Version: OS:

 

 [2015-06-16 18:12 UTC] chealer at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/control-structures.break
---
"break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of."

The documentation does not specify what happens if break's optional is not provided. Therefore, the effect of a "break;" statement inside several concerned structures (as happens at the end of the example) is unspecified. As of PHP 5.4, the effect appears to be that of "break 1;".

Comment http://php.net/manual/en/control-structures.break.php#90308 confirms.

Test script:
---------------
<?php
foreach(array(1, 2) as $iteration) {
	var_dump($iteration);
	foreach (array() as $dummy) {
		break;
	}
}
?>

Actual result:
--------------
int 1

int 2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-16 18:14 UTC] chealer at gmail dot com
Apologies, the result is the same, but the proper test should have read:

<?php
foreach(array(1, 2) as $iteration) {
	var_dump($iteration);
	foreach (array(1) as $dummy) {
		break;
	}
}
?>
 [2015-06-16 18:32 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-06-16 18:32 UTC] cmb@php.net
In my opinion, the documentation is pretty clear about that issue:

| break ends execution of the current for, foreach, while,
| do-while or switch structure.

Furthermore line 5 of the example is:

|         break;    /* You could also write 'break 1;' here. */
 [2015-06-27 18:45 UTC] chealer at gmail dot com
cmb, the sentence you quote is semantically invalid (there may be more than one current affected structure).

As for the example, it cannot replace specification (as for any example). Besides, the break statement you mention is in a single affected structure.

Thanks for reopening
 [2015-06-27 20:25 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=337067
Log: clarify break behavior unspecified when no argument is specified (doc bug #69853)
 [2015-06-27 20:26 UTC] salathe@php.net
-Status: Not a bug +Status: Closed
 [2015-06-27 20:26 UTC] salathe@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2015-06-27 21:57 UTC] chealer at gmail dot com
Thank you very much to you, salathe.

While the change does fix, I recommend to avoid referring to "the current loop", for 2 reasons:
break does not only break from loops.
When the behavior was unspecified, there are several current "loops", not just one.

I would suggest:
The default value is <literal>1</literal> (only break from the innermost enclosing structure).
 [2015-06-27 22:32 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=337069
Log: better phrasing (doc bug #69853)
 [2015-06-27 22:34 UTC] salathe@php.net
I've gone with "immediate enclosing structure". Hopefully that clears up any confusion.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 07:00:02 2026 UTC