Jump to content

[SOLVED] In what circumstances does (unset var) == 'string' ?


btherl

Recommended Posts

I have a very odd situation occurring in my code.  The entire app is around 50k LOC and uses postgres.

The situation only occurs when processing large amounts of data.

The code with unusual behaviour looks like this:

[code=php:0]if($_REQUEST['exceltype'] == 'yes'){[/code]


Now, $_REQUEST['exceltype'] is not set, as verified by var_dump($_REQUEST) inside this if statement.  BUT, the if statement succeeds!  The if statement fails if I use === instead of ==.

Is there any setting or circumstance in php which could cause unset values to evaluate as equal (in the == sense) to a string?

More information:  The code is running on the following system:
Server: Apache/1.3.33 (Debian GNU/Linux) mod_gzip/1.3.26.1a PHP/4.3.10-18 mod_auth_pam/1.1.1 mod_ssl/2.8.22 OpenSSL/0.9.7e mod_perl/1.29 mod_choke/0.06
Corbin, the point of my post is that they are not set to 'yes', but the expressions are still evaluating to true.  In fact, they are not set to any value.

If anyone can explain how this could happen, I am very interested to hear :)
I understand what you are saying Ken, but it doesn't answer my question.  It's not necessary to test for setness before making a comparison with == under usual circumstances, as null != 'yes'.

Try the following test script

[code=php:0]<?php
if ($a == 'yes') print "Unset variable is == to string constant\n";
?>[/code]
I tried your example and got the expected results, i.e. nothing printed. I also tried:
[code]<?php
$a = array();
if ($a['x'] == 'yes') print "Unset variable is == to string constant\n";
if ($_REQUEST['x'] == 'yes') print "Unset variable is == to string constant\n";
?>[/code]

And neither "if" resulted in anything being printed.

What is your configuration?

Ken
Here's what I'm using:

Server: Apache/1.3.33 (Debian GNU/Linux) mod_gzip/1.3.26.1a PHP/4.3.10-18 mod_auth_pam/1.1.1 mod_ssl/2.8.22 OpenSSL/0.9.7e mod_perl/1.29 mod_choke/0.06

I suspect I've found an obscure bug in php 4.3.10 .. I guess it's time to upgrade :)

The test script returns the expected results for me too.  It's only the application when running on certain data sets that results in the strange behaviour.

I've sat and stared at the output for quite some time, but no luck.. the if condition succeeds, but when I var_dump($_REQUEST) INSIDE the if, the tested array entry is not there.  I tried a few others, like [code=php:0]if ($_REQUEST['bubbles'] == 'bubbly')[/code] and they succeeded too.  All tests with === failed as expected, such as [code=php:0]if ($_REQUEST['bubbles'] === 'bubbly')[/code].

Thanks for your help guys :)  I'm going to file this under 'unreproducible bugs' (I found some others related to large arrays causing seg faults too, all fixed in php 5)
Nothing to do with your problem, but just some clarification.

[quote]It's not necessary to test for setness before making a comparison with == under usual circumstances[/quote]

Yes it is. Otherwise if you try and do a comparison on a variable that isn't set you generate a notice.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.