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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
38 - 9 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Thu Mar 28 18:01:29 2024 UTC