views:

115

answers:

4

Hey!

I got some legacy code that has this:

<?PHP
    if(isset($_GET['pagina'])=="homepage") {
?>
HtmlCode1
<?php 
} else { 
?>
HtmlCode2
<?php 
} 
?>

I don't know exactly why but this seems to be working. The htmlcode1 is loaded when I have ?pagina=homepage and the htmlcode2 is loaded when the pagina var doesn't exist or is something else (haven't really seen with something else, just not there). The website is using php4 (don't know the exact version). But really, how can this work? I looked at the manual and it says isset returns a bool..

Anyone?

+7  A: 

isset() returns true or false. In a boolean comparison, "homepage" would evaluate to true. So essentially you got here:

if ( isset($_GET['pagina']) == true )

If pagina equals anything, you will see HtmlCode1. If it is no set, you will see HtmlCode2.

I just tried it to confirm this, and going to ?pagina=somethingelse does not show HtmlCode2.

+4  A: 

I suspect that it's a bug as it doesn't really make sense to compare true/false with "homepage". I would expect the code should actually be:

if (isset($_GET['pagina']) && ($_GET['pagina'] == "homepage")) {
}
Stacey Richards
A: 

Some ideas how this could work (apart from the previously mentioned "homepage"==true):

  • Isset has been redefined somewhere?
  • It's a self-modified version of PHP?
Vilx-
+3  A: 

The problem is that "==" isn't a type-sensitive comparison. Any (non-empty) string is "equal" to boolean true, but not identical to it (for that you need to use the "===" operator).

A quick example, why you're seeing this behavior:
http://codepad.org/aNh1ahu8

And for more details about it from the documentation, see:
http://php.net/manual/en/language.operators.comparison.php
http://ca3.php.net/manual/en/types.comparisons.php (the "Loose comparisons with ==" table specifically)

Chad Birch
Thanks! This must be why it is working
AntonioCS