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

David L. Anselmi anselmi at anselmi.us
Mon Sep 26 16:37:50 MDT 2005


I guess you figured this out, but since I didn't see a concise 
explanation here's a try.  You made the same mistake that perl coders 
make if they don't understand the difference between || and or.

Jed S. Baer wrote:
>     $ptologfile = fopen('/tmp/contactlog.txt', 'a') || die ("Can't open
> logfile");
>     if (is_writable('/tmp/contactlog.txt')) echo '<b>Writeable</b><br>';
>     $junk = 'junk';
>     fwrite($ptologfile, $junk) || die ("Can't write to logfile");
[...]
> fwrite(): supplied argument is not a valid stream resource

This says that $ptologfile is bogus implying that fopen() failed, but 
you tested it and it didn't.

If you look here http://us2.php.net/manual/en/language.operators.php you 
see that = is lower precedence than ||.  That means that:

 > $ptologfile = fopen('/tmp/contactlog.txt', 'a') || die ("Can't open
 > logfile");

assigns TRUE to $ptologfile, which is bogus for fwrite().

The correct way is:

 > $ptologfile = fopen('/tmp/contactlog.txt', 'a') or die ("Can't open
 > logfile");

because or is lower precedence than =.

HTH,
Dave
_______________________________________________
CLUE-tech mailing list
CLUE-tech at cluedenver.org
http://cluedenver.org/mailman/listinfo/clue-tech



More information about the clue-tech mailing list