Edit report at http://bugs.php.net/bug.php?id=52031&edit=1
ID: 52031 Updated by: degeb...@php.net Reported by: d dot reade at ReadesGroupServices dot com Summary: Passing "0" as a parameter through a custom method Status: Bogus Type: Bug Package: Unknown/Other Function Operating System: CentOS 5.5 PHP Version: 5.3.2 New Comment: You are comparing an integer with a string, so the string gets converted to an int. Because (int)"Test" is (int)0 and 0==0, the condition passes. It's not a bug, and it's documented behavior. If you want it to check the type as well, you need to use the === operator as documented. Previous Comments: ------------------------------------------------------------------------ [2010-06-09 14:57:52] d dot reade at ReadesGroupServices dot com Sorry but that makes no sense. How is this not a bug? If I pass "0" as the parameter and the first if statement is empty(), PHP knows the paramater is "empty" and the if statement is triggered. Whereas without empty() PHP executes the first statement regardless even though the same value would trigger empty()! It shouldn't do that - if the first if statement is ($str == 'Test'), PHP should skip it just as it would with any other string that doesn't match. ------------------------------------------------------------------------ [2010-06-09 14:51:46] degeb...@php.net Sorry, but your problem does not imply a bug in PHP itself. For a list of more appropriate places to ask for help using PHP, please visit http://www.php.net/support.php as this bug system is not the appropriate forum for asking support questions. Due to the volume of reports we can not explain in detail here why your report is not a bug. The support channels will be able to provide an explanation for you. Thank you for your interest in PHP. See: http://php.net/manual/en/language.types.type-juggling.php ------------------------------------------------------------------------ [2010-06-09 14:41:32] d dot reade at ReadesGroupServices dot com Description: ------------ Sorry but I didn't know how to best word the summary. I have a custom method with an if statement inside. If the parameter passed through matches "Test", a string is echo'ed. However if I pass "0" through as the parameter, PHP thinks this is "Test" and echoes the string. Passing a blank parameter through, i.e. "", does not have the same effect as "0". Inserting "if (empty($str))" before the if statement in the example works as expected, catches the "0" parameter and I get the expected result. However without this, I get the actual result. All plugins have been disabled and Apache restarted. Test script: --------------- <?php function testMethod($str = 0) { if ($str == 'Test') { echo 'Test String!<br>'; } } testMethod('Test'); testMethod(0); testMethod(); testMethod(1); testMethod('Something'); ?> Expected result: ---------------- Test String! Actual result: -------------- Test String! Test String! Test String! ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52031&edit=1