php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52265 Defaulting a class-function-param to 0 makes an IF see it as true when comparin
Submitted: 2010-07-06 16:42 UTC Modified: 2010-07-06 17:17 UTC
From: weer1 at gmx dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.13 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: weer1 at gmx dot net
New email:
PHP Version: OS:

 

 [2010-07-06 16:42 UTC] weer1 at gmx dot net
Description:
------------
Defaulting a class-function-param to 0 makes an IF see it as true when comparing it to a string.

	function fillSelectBox($selected = 0)
	{
		$txt = '';

		$obj = new RetailerCountry();
		$objs = $obj->getAll();
		foreach ($objs as $ob)
		{
			$id = $ob->getAttrib('retailercountry');
			$name = $ob->getAttrib('retailercountry');

			$txt .= $ob->getAttrib('retailercountry');

			echo 'i:'.$id;
			echo 's:'.$selected;

			if ($id == $selected)
			{
				$txt .= ' * ';
			}
			$txt .= '<br>';
		}

		return $txt;
	}

The result for this function is: every word that's displayed gets a star *.
This changes when I change the input param to $selected = ''.

But did or does PHP every care about this? I don't to do some kind of strict typing!

Regards
MD


Test script:
---------------
	function fillSelectBox($selected = 0)
	{
		$txt = '';

		$obj = new RetailerCountry();
		$objs = $obj->getAll();
		foreach ($objs as $ob)
		{
			$id = $ob->getAttrib('retailercountry');
			$name = $ob->getAttrib('retailercountry');

			$txt .= $ob->getAttrib('retailercountry');

			echo 'i:'.$id;
			echo 's:'.$selected;

			if ($id == $selected)
			{
				$txt .= ' * ';
			}
			$txt .= '<br>';
		}
		return $txt;
	}


Expected result:
----------------
The result for this function is: every word that's displayed gets a star *.
This changes when I change the input param to $selected = ''.

But did or does PHP every care about this? I don't to do some kind of strict typing!


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-06 17:17 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-07-06 17:17 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See http://php.net/manual/en/types.comparisons.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 16:01:33 2025 UTC