Req #76500 [Com]: fputcsv() is ignoring the value FALSE
| From: | spam2 at rhsoft dot net | Date: | Tue, 19 Jun 2018 18:19:15 +0000 |
| Subject: | Req #76500 [Com]: fputcsv() is ignoring the value FALSE | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215798@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
Comment by: spam2 at rhsoft dot net
Reported by: thomas at landauer dot at
Summary: fputcsv() is ignoring the value FALSE
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:
> I guess we all agree that the current mapping isn't quite smart
no, because there is no mapping at all, it's that easy
that's how implicit type casting works
if you touch that here and make special cases you open a can of worms
Previous Comments:
------------------------------------------------------------------------
[2018-06-19 18:13:56] thomas at landauer dot at
> The quotes are not part of the string
Yeah, you're right, sorry! The quotes are added only if the string contains special characters.
> echo (int)NULL;
Yeah ;-)
But let's see it from a different perspective:
In the database, we have three possible values: true, false, null
In the CSV file, we have three options: 1, 0, nothing
I guess we all agree that the current mapping isn't quite smart:
true => 1
false, null => nothing
So what I'm asking for is *some* way to get:
true => 1
false => 0
null => nothing
------------------------------------------------------------------------
[2018-06-19 17:11:08] spam2 at rhsoft dot net
and if you want to distinct between false and NULL you have to be prepared to do that by hand
anyways in the export as well as in the import because your "So if you have a nullable boolean
field in your database, this function becomes completely useless, since you can't tell
null from false" is pointless too
php > echo (int)false;
0
php > echo (int)NULL;
0
------------------------------------------------------------------------
[2018-06-19 17:07:22] nikic@php.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).
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.
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
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