[clue-tech] I'm blind, or why is this PHP fwrite() failing?

Jed S. Baer thag at frii.com
Sun Sep 25 22:34:20 MDT 2005


On Sun, 25 Sep 2005 20:17:42 -0600
David L. Anselmi wrote:

> I'd have guessed that perhaps fwrite returned something useful that 
> wasn't true.

Well, the invalid stream implies that the fopen failed. fopen() returns
either a file handle or boolean FALSE.

fwrite() returns the number of bytes written, or FALSE on error.

Just for the sake of being completely nutso, I figured I'd try the example
exactly as in the docs, substituting only string values. It works. So I
guess I suffer from some sort of boolean half-dyslexia, since the two
constructs:

if (fwrite($handle, $somecontent) === FALSE) {
   echo "Cannot write to file ($filename)";
   exit;
}

and 

fwrite( ... ) || die ('message');

seem to me to be logically equivalent.

However, reverting to my orginal code, sans the "or die" part of the
fwrite line, yields success, meaning that the file being created and left
empty is the result of not closing the stream. I'd've thought PHP was a
bit smarter in it's program termination code, but I guess not. So, off I
go (but probably tomorrow) to find documentation of PHP's treatment of
BOOLEAN casts from integers. FWIW, I've seen the "or die" construct used a
lot in example code from supposedly experienced PHP coders. Sigh.

jed
-- 
http://s88369986.onlinehome.us/freedomsight/
Key fingerprint = B027 FEFB 4281 CC72 67D1  4237 F2D0 D356 077A A30E
... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier
_______________________________________________
CLUE-tech mailing list
CLUE-tech at cluedenver.org
http://cluedenver.org/mailman/listinfo/clue-tech



More information about the clue-tech mailing list