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

Collins Richey crichey at gmail.com
Sun Sep 25 22:42:26 MDT 2005


On 9/25/05, Jed S. Baer <thag at frii.com> wrote:
> 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.

I seem to remember from elsewhere that the === FALSE construct is the
only bullet proof way to check. Yes, I've seen a lot of the || die
constructs. Just goes to prove that you can't always rely on the
samples. Sigh also.


--
Collins Richey
      Debugging is twice as hard as writing the code ... If you write
      the code as cleverly as possible, you are, by definition, not
      smart enough to debug it.
             -Brian Kernighan
_______________________________________________
CLUE-tech mailing list
CLUE-tech at cluedenver.org
http://cluedenver.org/mailman/listinfo/clue-tech



More information about the clue-tech mailing list