php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52031 Passing "0" as a parameter through a custom method
Submitted: 2010-06-09 14:41 UTC Modified: 2010-06-09 15:16 UTC
From: d dot reade at ReadesGroupServices dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.2 OS: CentOS 5.5
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: d dot reade at ReadesGroupServices dot com
New email:
PHP Version: OS:

 

 [2010-06-09 14:41 UTC] 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!

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-09 14:51 UTC] degeberg@php.net
-Status: Open +Status: Bogus
 [2010-06-09 14:51 UTC] degeberg@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:57 UTC] 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 15:14 UTC] degeberg@php.net
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.
 [2010-06-09 15:16 UTC] d dot reade at ReadesGroupServices dot com
I see you what mean. Thanks for the clarification, much appreciated.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 05 17:01:34 2025 UTC