Req #76500 [ReO->Nab]: fputcsv() is ignoring the value FALSE
| From: | nikic@php.net | Date: | Tue, 19 Jun 2018 17:07:23 +0000 |
| Subject: | Req #76500 [ReO->Nab]: fputcsv() is ignoring the value FALSE | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215795@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76500&edit=1
ID: 76500
Updated by: nikic@php.net
Reported by: thomas at landauer dot at
Summary: fputcsv() is ignoring the value FALSE
-Status: Re-Opened
+Status: Not a bug
Type: Feature/Change Request
Package: Filesystem function related
Operating System: Linux Mint
PHP Version: 7.1.18
Block user comment: N
Private report: N
New Comment:
> CSV (and
fputcsv()) does have a distinction between strings and
> numbers: the quotes! true is exported as
> 1, not as "1". Why?? =>
> The only possible answer I see: Because it's casted to int (not
> to string).
Such a distinction does not exist. The quotes are not part of the string (unless you explicitly
include them), so obviously they will not be printed. In PHP, (string)false=="". That may
or may not be a good choice, but it's the way it is and not going to change. fputcsv() does
nothing more and nothing less than a simple string cast before printing the value. If you want to
deviate from the standard string casting logic, you need to do so yourself.
Previous Comments:
------------------------------------------------------------------------
[2018-06-19 17:06:06] spam2 at rhsoft dot net
but csv is all about TEXT and not numbers, so there is no point that something is casted to int - if
you want
http://php.net/manual/en/language.types.type-juggling.php
if you want to avoid this you have to cast your values explicit, that's it
------------------------------------------------------------------------
[2018-06-19 17:01:22] thomas at landauer dot at
You don't understand what I mean ;-)
php > echo (int)true;
1
php > echo (int)false;
0
------------------------------------------------------------------------
[2018-06-19 16:55:19] spam2 at rhsoft dot net
> CSV (and fputcsv()) does have a distinction
> between strings and numbers: the quotes! true
> is exported as 1, not as
> "1". Why?? => The
> only possible answer I see: Because it's casted
> to int (not to string)
you don't understand how PHP works
php > echo (string)true;
1
php > echo (string)false;
php >
------------------------------------------------------------------------
[2018-06-19 16:30:14] thomas at landauer dot at
> CSV knows nothing about NULLs and booleans â it's all about
strings
Just reasoned about this some more. And I came to the conclusion that it isn't true ;-)
CSV (and fputcsv()) does have a distinction between strings and numbers: the quotes!
true is exported as 1, not as "1". Why?? => The
only possible answer I see: Because it's casted to int (not to
string).
Well, and if you cast false to an int, what do you get? =>
0!
------------------------------------------------------------------------
[2018-06-19 15:52:31] thomas at landauer dot at
> And you won't be able to distinguish either from an empty string.
Well, an empty string is not possible in a boolean field ;-)
> In my opinion, adding yet another optional argument to fputcsv()
does more harm than good.
Why? Which harm?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=76500
--
Edit this bug report at https://bugs.php.net/bug.php?id=76500&edit=1