php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53406 inconsistent behavior with booleans in writeElement() method
Submitted: 2010-11-25 20:22 UTC Modified: 2010-11-25 21:24 UTC
From: dubois dot benjamin at me dot com Assigned:
Status: Not a bug Package: XML Writer
PHP Version: 5.3.3 OS: ANY
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: dubois dot benjamin at me dot com
New email:
PHP Version: OS:

 

 [2010-11-25 20:22 UTC] dubois dot benjamin at me dot com
Description:
------------
I use xmlWriter a lot, but I found a quite inconsistent behavior with booleans in 
xmlWriter. 

When filling an element/namespace with a boolean : 

- if the boolean is true, it is automatically casted to an int (1), which is a 
legal representation of the boolean. 
- if the boolean is false, no value is given to the element/attribute. 

I made a little script to reproduce the problem.It runs well in CLI

Test script:
---------------
<?php

xml(true);
xml(false);

function xml($bool) {
	$xml = new XmlWriter();
	$xml->openMemory();
	$xml->setIndent(true);
	$xml->startElement('root');
	$xml->writeAttribute('boolAttr', $bool);
	$res = $xml->writeElement('myboolean',$bool);
	$xml->endElement();
	echo $xml->flush().PHP_EOL;
	var_dump($res);
}

Expected result:
----------------
<root boolAttr="1">
 <myboolean>1</myboolean>
</root>

bool(true)
<root boolAttr="0">
 <myboolean>0</myboolean>
</root>

Actual result:
--------------
<root boolAttr="1">
 <myboolean>1</myboolean>
</root>

bool(true)
<root boolAttr="">
 <myboolean></myboolean>
</root>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-25 21:24 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2010-11-25 21:24 UTC] cataphract@php.net
This is not inconsistent, it follows the general rules used for boolean -> string conversions in PHP:

>php -r "echo false, \"\ndone\";"

done
>php -r "echo true, \"\ndone\";"

1
done
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 27 16:01:27 2024 UTC