Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 7.2.25 FALSE != false?

Tags:

php

swig

Can anyone explain this? I assume I'm doing something incredibly boneheaded, and am eagerly awaiting enlightenment.

<?php
echo "phpversion() == " . phpversion() . "\n\n";

var_dump(FALSE);
var_dump(False);
var_dump(false);

echo "\n";
echo "(FALSE === false) == " . ((FALSE === false) ? "true" : "false") . "\n";
echo "(((bool)FALSE) === false) == " . ((((bool)FALSE) === false) ? "true" : "false") . "\n";
?>

I'm getting this output:

phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108445

int(0)
bool(false)
bool(false)

(FALSE === false) == false
(((bool)FALSE) === false) == true

The reason I care is, the documentation for fopen() says it returns FALSE on failure. But, at least in this build of PHP, it's returning false, so my if ($fh === FALSE) check is failing, making me think the fopen() succeeded, even though it definitely failed.

My friend is running a different build (PHP 7.2.24 on Ubuntu), and there, FALSE === false. I assume everything everywhere would explode if FALSE != false, so... what the heck is going on?

like image 949
kuhrusty Avatar asked Mar 04 '23 02:03

kuhrusty


1 Answers

I have not figured out the cause yet, but it is in one of two PHP extensions I built myself: remove them, and it works as expected:

phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108445

bool(false)
bool(false)
bool(false)

(FALSE === false) == true
(((bool)FALSE) === false) == true

I may add the details of how I managed to accidentally redefine FALSE in a PHP extension; depends on how embarrassing they are.

EDIT: I'm still looking at the details, but the short version is, I used SWIG to build a wrapper around libmodbus, and its src/modbus.h has:

#ifndef FALSE
#define FALSE 0
#endif

... which is not unreasonable, but I lazily included "everything" from libmodbus rather than specifying the individual functions I wanted to expose, and I'm guessing that's how FALSE got in there. (However, the resulting .so which gets deployed as the PHP extension doesn't seem to contain a symbol with FALSE in the name, so... I'm still looking.)

EDIT 2: Yeah, just having a #define in your SWIG .i is enough to have SWIG define it as a constant for use in PHP. (Which makes sense.)

Incidentally, the section of the SWIG documentation which might have steered me a little wrong (having me tell SWIG to include all of libmodbus' headers instead of specifying each function) was "SWIG for the truly lazy." Look, when I see a section which looks like it was written for me, that's what I'm reading.

like image 199
kuhrusty Avatar answered Mar 05 '23 14:03

kuhrusty