php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70132 bit(1) always seen as true
Submitted: 2015-07-24 16:56 UTC Modified: 2021-03-02 13:35 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: stanimir at datacentrix dot org Assigned: cmb (profile)
Status: Closed Package: PDO MySQL
PHP Version: 5.6.11 OS: linux
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:
14 + 8 = ?
Subscribe to this entry?

 
 [2015-07-24 16:56 UTC] stanimir at datacentrix dot org
Description:
------------
Using MySQL 5.5.44, BIT(1) column returns either 0x00 or 0x01,
both are considered as true by the language.

Test script:
---------------
var_dump((bool) chr(0x00));

Expected result:
----------------
boolean false

Actual result:
--------------
boolean true

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-25 21:45 UTC] fontanalorenz at gmail dot com
AFAIK the expected result is true because when 0x00 is converted by chr gives you a one char string so when you cast to bool it returns true;

To verify that you can just do

<?php

$chr = chr(0x00);

var_dump((bool)$chr); // bool(true)
var_dump(strlen($chr)); // int(1)
 [2021-03-02 13:35 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-03-02 13:35 UTC] cmb@php.net
Indeed, casting a string with a NUL byte to bool is supposed to
return true.  No issue here.

Anyhow, it appears this issue has been fixed in the meantime. With
emulated prepares, I get `string(1) "0"` and `string(1) "1"`,
respectively, and without emulated prepares I get int(0)/int(1).
So casting to bool yields the desired result.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Apr 16 15:01:24 2021 UTC