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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: stanimir at datacentrix dot org
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 01:01:28 2024 UTC