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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

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-2018 The PHP Group
All rights reserved.
Last updated: Fri Nov 16 03:01:26 2018 UTC