#47576 [Fbk->Opn]: "continue i;" working wrong on foreach

From: Date: Tue, 10 Mar 2009 10:42:39 +0000
Subject: #47576 [Fbk->Opn]: "continue i;" working wrong on foreach
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-134551@lists.php.net to get a copy of this message
ID: 47576 User updated by: fischikowski at web dot de Reported By: fischikowski at web dot de -Status: Feedback +Status: Open Bug Type: Scripting Engine problem Operating System: Windows Vista 32bit PHP Version: 5.2.9 New Comment: Except from the "<?php" and "?>" I already posted the shorter script. <?php function test() { $array = array("test"); foreach($array as $element) { for($j=0;$j<5;$j++) { continue 2; } //inserting code here affects return-value return true; } return false; } echo test()?"true":"false"; ?> Previous Comments: ------------------------------------------------------------------------ [2009-03-10 10:34:43] jani@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2009-03-06 08:30:09] fischikowski at web dot de I could shorten the test-code (the previous was my original working code): function test() { $array = array("test"); foreach($array as $element) { for($j=0;$j<5;$j++) { continue 2; } return true; } return false; } echo test()?"true":"false"; I expect, that this returns false, because "continue 2" should step to the next foreach-loop (and "return true" should not be executed). But it happens, that true is returned instead. If you insert echo ""; in front of the "return true" everything works fine again (it then returns false). I also noticed, that the outer loop has to be a foreach-loop, if it was a for-loop the result would be also correct. ------------------------------------------------------------------------ [2009-03-05 17:34:29] fischikowski at web dot de Description: ------------ The echo ""; affects the "return true;" below. As long as the echo is there everything works as expected, if you put "//" in front of the echo, the return will be called even if it should not. Reproduce code: --------------- function matchHostList($host, $list_file) { $host = explode(".", $host); //$list_contents = explode("\n", file_get_contents($list_file)); $list_contents = array("www.google.de"); foreach($list_contents as $list_host) { $list_host = explode(".", rtrim($list_host)); //if the list-host is more specific than the tested host we can't match if(count($host) < count($list_host)) continue; for($i = 1;$i<=count($list_host) && $i <= count($host);$i++) { if($list_host[count($list_host)-$i] != $host[count($host)-$i]) continue 2; } echo ""; //this is necessary to avoid wrong return return true; } return false; } echo matchHostList("false.google.de", "")?"true":"false"; Expected result: ---------------- When running as shown above this returns false (because "continue 2;" continues the loop above the "return true;"), this is OK. If you remove the echo ""; above the "return true;" You would expect that nothing changes (because we only removed some null-output), but in fact the return value changes to true. Actual result: -------------- see Expected result ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=47576&edit=1

« previous php.bugs (#134551) next »