Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Last time Append line to text file

0 views
Skip to first unread message

Rodrigo

unread,
Apr 28, 2002, 11:55:04 PM4/28/02
to php-g...@lists.php.net, Miguel Cruz, John Holmes
Ok, but I'm trying the following code wich is basicaly the same Miguel
sent me and I still get the same message.
Please excuseme for being so repetitive but Í don't seem to see where is
the mistake, Sorry guys for the trouble.

<?php

$file_pointer = fopen('/public_html/emails.txt', "a") || exit;
$string_to_write = ("$newemail") . "\n";
fwrite($file_pointer, $string_to_write);
fclose($file_pointer);

?>



Warning: Supplied argument is not a valid File-Handle resource in
/home/restricted/home/h4ck3r/public_html/write.php on line 4

Warning: Supplied argument is not a valid File-Handle resource in
/home/restricted/home/h4ck3r/public_html/write.php on line 6

Richard Emery

unread,
Apr 29, 2002, 12:25:35 AM4/29/02
to Rodrigo, php-g...@lists.php.net
The answer is:
$file_pointer = fopen('/public_html/emails.txt', "a") or exit;


The " || " is a binary operation. You want "or", the logical operation.

Miguel Cruz

unread,
Apr 29, 2002, 12:33:43 AM4/29/02
to Richard Emery, php-g...@lists.php.net
On Sun, 28 Apr 2002, Richard Emery wrote:
> The answer is:
> $file_pointer = fopen('/public_html/emails.txt', "a") or exit;
>
> The " || " is a binary operation. You want "or", the logical operation.

Yeah, Perl habit.

But || is logical too (it's | that's binary), it just seems to result in
$file_pointer being recast from 'resource' to 'boolean' for some reason.

This side effect is not mentioned at
http://www.php.net/manual/en/language.operators.logical.php where it
imples that the only difference is precedence. I'd say this is a bug
either in the manual or in PHP itself.

miguel

> ----- Original Message -----
> From: Rodrigo <tag...@tagzero.com>
> To: <php-g...@lists.php.net>; 'Miguel Cruz' <m...@stoic.net>; 'John
> Holmes' <holmes...@charter.net>
> Sent: Sunday, April 28, 2002 10:55 PM
> Subject: [PHP] Last time Append line to text file
>
>
> Ok, but I'm trying the following code wich is basicaly the same Miguel
> sent me and I still get the same message.

> Please excuseme for being so repetitive but Ķ don't seem to see where is

Martin Towell

unread,
Apr 29, 2002, 12:36:57 AM4/29/02
to php-g...@lists.php.net
or you could code it like this:

$file_pointer = fopen('/public_html/emails.txt', "a")
if (!$file_pointer) exit;

Yeah, Perl habit.

miguel

> Please excuseme for being so repetitive but Í don't seem to see where is


> the mistake, Sorry guys for the trouble.
>
> <?php
>
> $file_pointer = fopen('/public_html/emails.txt', "a") || exit;
> $string_to_write = ("$newemail") . "\n";
> fwrite($file_pointer, $string_to_write);
> fclose($file_pointer);
>
> ?>
>
>
>
> Warning: Supplied argument is not a valid File-Handle resource in
> /home/restricted/home/h4ck3r/public_html/write.php on line 4
>
> Warning: Supplied argument is not a valid File-Handle resource in
> /home/restricted/home/h4ck3r/public_html/write.php on line 6
>
>
>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Mike Ford

unread,
Apr 29, 2002, 9:03:13 AM4/29/02
to Miguel Cruz, Richard Emery, php-g...@lists.php.net
> -----Original Message-----
> From: Miguel Cruz [mailto:m...@stoic.net]
> Sent: 29 April 2002 05:34
>
> On Sun, 28 Apr 2002, Richard Emery wrote:
> > The answer is:
> > $file_pointer = fopen('/public_html/emails.txt', "a") or exit;
> >
> > The " || " is a binary operation. You want "or", the
> logical operation.
>
> Yeah, Perl habit.
>
> But || is logical too (it's | that's binary), it just seems
> to result in
> $file_pointer being recast from 'resource' to 'boolean' for
> some reason.
>
> This side effect is not mentioned at
> http://www.php.net/manual/en/language.operators.logical.php where it
> imples that the only difference is precedence. I'd say this is a bug
> either in the manual or in PHP itself.

Surely this is *precisely* due to the difference in precedence. The or operator has lower precedence than =, whereas || has higher precedence, so:

$file_pointer = fopen(...) or exit;
would be ($file_pointer = fopen(...)) or exit;

(i.e. evaluate the assignment expression first, and if it returns a false value then exit)

whereas

$file_pointer = fopen(...) || exit;
would be $file_pointer = (fopen(...) || exit);

(i.e. evaluate the Boolean or first, casting the fopen() result in the process, then assign the result to $file_pointer (assuming exit hasn't already terminated execution by then!)).

So, no bug, no unexpected side-effects, just a logical result of applying the precedence rules strictly as advertised!

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.f...@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

Miguel Cruz

unread,
Apr 29, 2002, 1:13:55 PM4/29/02
to Ford, Mike [LSS], php-g...@lists.php.net
On Mon, 29 Apr 2002, Ford, Mike [LSS] wrote:
>> From: Miguel Cruz [mailto:m...@stoic.net]
>>
>> This side effect is not mentioned at
>> http://www.php.net/manual/en/language.operators.logical.php where it
>> imples that the only difference is precedence. I'd say this is a bug
>> either in the manual or in PHP itself.
>
> So, no bug, no unexpected side-effects, just a logical result of
> applying the precedence rules strictly as advertised!

Sounds good to me! I withdraw my aspersion.

miguel

0 new messages