php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48192 if structure inherit else
Submitted: 2009-05-08 17:04 UTC Modified: 2020-04-07 09:43 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: skds1433 at hotmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2009-05-08 17:04 UTC] skds1433 at hotmail dot com
Description:
------------
I'm requesting a way for the 'if' control struction to be able to inherit 'else' control structures, in a similar matter as below. This will help prevent duplicated code, and help keep it clean and uncluttered.

I've included an 'expected_equivalent', which is actually less code than in the 'routine' function. However, if a developer wishes to include a greater diveristy, it can become a real advantage to use just one 'else'.

Additionally, it I think it would be benefitional for 'if' control structure to make it more 'complete'.

I also understand there are already numerous ways to do this, which have the same result and possibly even less code. Nonetheless, I believe this way could be a better, easier way for certain situations.

Reproduce code:
---------------
<?php
function routine ($foo, $bar)
{
	if ($foo === 0) echo "foo is 0\n";
	elseif ($foo === 1)
		if ($bar === 0) echo "bar is 0\n";
		elseif ($bar === 1) echo "bar is 1\n";
			else echo "foo: ".$foo.", bar: ".$bar."\n";
			# else would apply to both elseif
}

function expected_equivalent ($foo, $bar)
{
	if ($foo === 0) echo "foo is 0\n";
	elseif ($foo === 1 && $bar === 0) echo "bar is 0\n";
	elseif ($foo === 1 && $bar === 1) echo "bar is 1\n";
	else echo "foo: ".$foo.", bar: ".$bar."\n";
}

routine (0, 1);
routine (1, 0);
routine (1, 1);
routine (2, 1);
routine (1, 2);
routine (2, 2);
?>

Expected result:
----------------
foo is 0
bar is 0
bar is 1
foo: 2, bar: 1
foo: 1, bar: 2
foo: 2, bar: 2

Actual result:
--------------
foo is 0
bar is 0
bar is 1
foo: 1, bar: 2

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-08 18:39 UTC] random-passer-by at hello dot world
This is a twist on the Dangling Else problem:
http://en.wikipedia.org/wiki/Dangling_else

I find your solution more confusing and incompatible than what I've seen in any other other programming language.

This should achieve the correct result and avoid the extra foo comparisons without a language change:

function routine ($foo, $bar)
{	
	do {
		if ($foo === 0) { echo "foo is 0\n"; break; }
		elseif ($foo === 1)
			if ($bar === 0) { echo "bar is 0\n"; break; }
			elseif ($bar === 1) { echo "bar is 1\n"; break; }
		echo "foo: ".$foo.", bar: ".$bar."\n";
	} while (0);
}
 [2009-05-08 20:47 UTC] skds1433 at hotmail dot com
To me, the whole 'dangling else' is a paradox.

Wouldn't it seem more complete if that dangling 'else' was interpreted for both 'if'?

If 'if a then if b then s1 else s2' was interpreted like this (below), I personally see no paradox. It just seems to me, like a paradox for the fact that this isn't how it's interpreted.

if (a)
{
	if (b)
	{
		s1
	}
	else
	{
		s2
	}
}
else
{
	s2
}

I'm not looking for a solution, since there's probably 20 different ways to solve that. I'm just suggesting what might be a better way.
 [2011-04-08 18:19 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: vista +Operating System: * -PHP Version: 5.2.9 +PHP Version: *
 [2020-04-07 09:43 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-04-07 09:43 UTC] cmb@php.net
I'm pretty sure that the current interpretation of dangling else
will never change (not only for BC reasons).  If you still like it
to, please pursue the RFC process[1].

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 14:01:29 2024 UTC