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

From: Date: Mon, 06 Apr 2009 12:52:07 +0000
Subject: #47576 [Opn->Fbk]: "continue i;" working wrong on foreach
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-135580@lists.php.net to get a copy of this message
 ID:               47576
 Updated by:       jani@php.net
 Reported By:      fischikowski at web dot de
-Status:           Open
+Status:           Feedback
 Bug Type:         Scripting Engine problem
 Operating System: Windows Vista 32bit
 PHP Version:      5.2.9
 New Comment:

Works fine on windows, linux 32/64bit for me.


Previous Comments:
------------------------------------------------------------------------

[2009-03-10 14:43:21] fischikowski at web dot de

I don't think to use any. I am using PHP on an apache-webserver, if
that makes a difference.

Could also not reproduce this on linux with Version 5.2.6, had no
higher versions on a linux system to test it. Will try that later.

------------------------------------------------------------------------

[2009-03-10 13:51:43] scottmac@php.net

I can't reproduce this on Linux or OS X

Do you have any Zend Extensions enabled such as eAccelerator, APC, Zend
Optimizer or anything like that?

------------------------------------------------------------------------

[2009-03-10 10:42:39] fischikowski at web dot de

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

------------------------------------------------------------------------

[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



Thread (11 messages)

« previous php.bugs (#135580) next »