Use ... === false instead of ! ...

Locked
WaldoMonster
getID3() contributor
Posts:112
Joined:Mon Apr 01, 2002 12:20 am
Location:Netherlands
Contact:
Use ... === false instead of ! ...

Post by WaldoMonster » Sat Feb 25, 2012 10:53 am

From the PHP website:
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
The function returns the number of bytes that were written to the file, or FALSE on failure.

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Code: Select all

if (!file_put_contents($dest, $data)) {
    throw new Exception('failed to create file '.$dest);
I think this and simular code shoud be changed in:

Code: Select all

if (file_put_contents($dest, $data) === false) {
    throw new Exception('failed to create file '.$dest);
Or is this to paranoia because files from 0 bytes are not possible?

James Heinrich
getID3() v1 developer
Posts:1477
Joined:Fri May 04, 2001 4:00 pm
Are you a spambot?:no
Location:Northern Ontario, Canada
Contact:

Re: Use ... === false instead of ! ...

Post by James Heinrich » Sat Feb 25, 2012 1:11 pm

WaldoMonster wrote:Or is this to paranoia because files from 0 bytes are not possible?
I'd have to look at all the instances where that code exists, but most likely it's fine the way it is: either it failed to access the file for writing, or something else failed and there's no data to write, either way we have a problem.

But still, it may make more sense to write it correctly.

James Heinrich
getID3() v1 developer
Posts:1477
Joined:Fri May 04, 2001 4:00 pm
Are you a spambot?:no
Location:Northern Ontario, Canada
Contact:

Re: Use ... === false instead of ! ...

Post by James Heinrich » Sat Feb 25, 2012 1:14 pm

Actually, it turns out that that block of code has already been rewritten in the latest development version.

WaldoMonster
getID3() contributor
Posts:112
Joined:Mon Apr 01, 2002 12:20 am
Location:Netherlands
Contact:

Re: Use ... === false instead of ! ...

Post by WaldoMonster » Sat Feb 25, 2012 1:20 pm

James Heinrich wrote:Actually, it turns out that that block of code has already been rewritten in the latest development version.
Thanks for the info.

Locked