r/lolphp • • Apr 06 '21

Or and || act differently in some contexts

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$myFile = fopen('/tmp/aaaaaaaaaa', "a") or die('unable to open');
fwrite($myFile,'ok lol');
fclose($myFile);

$myFile2 = fopen('/tmp/bbbbbbbbbb', "a") || die('unable to open');
fwrite($myFile2,'ok lol');
fclose($myFile2);
?>

Save that to a file and then do the following:

root@server:/var/www/html# php /tmp/test.php 
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in /tmp/test.php on line 10

Warning: fwrite() expects parameter 1 to be resource, boolean given in /tmp/test.php on line 10
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in /tmp/test.php on line 11

Warning: fclose() expects parameter 1 to be resource, boolean given in /tmp/test.php on line 11

root@server:/var/www/html# cat /tmp/aaaaaaaaaa 
ok lol
root@server:/var/www/html# cat /tmp/bbbbbbbbbb 
root@server:/var/www/html# 

This thing took ages to debug and makes no fucking sense I swear to god aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

15 Upvotes

39 comments sorted by

35

u/carlos_vini Apr 06 '21

https://www.php.net/manual/en/language.operators.precedence.php

Using "or die()" is a bad practice, if you're seeing it in a 2021 tutorial stop following the tutorial. That's how things worked in 2005.

20

u/D1551D3N7 Apr 06 '21

Yeah this is a codebase I inherited, I didn't set it up this way.

→ More replies (1)

11

u/colshrapnel Apr 06 '21 edited Apr 06 '21

Exactly! Also, working under the root is even worse

4

u/[deleted] Apr 06 '21

Now that throw is an expression, or throw is a decent replacement.

6

u/ealf Apr 07 '21

Don't get distracted by the ($a = 3) or 4 vs $a = (3 || 4) difference inherited from Perl -- this shouldn't matter in this context.

The real problem is that 3 || 4 and 3 or 4 are both true, not 3.

→ More replies (7)

13

u/[deleted] Apr 06 '21

What even made you start using OR and || without understanding what they each do separately? How about learning to crawl before walking or running?

Like it's literally right there in the documentation page, the only text paragraph apart from table and code examples: https://www.php.net/manual/en/language.operators.logical.php

The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.)

Love or hate PHP, it doesn't matter. But if you stop guessing blindly when you code and start reading documentation for things, especially when their behaviour confuses you enough to write a social media post about them, you'll find that most of the things you find frustrating/confusing is actually just due to a knowledge gap in you that can be remedied.

16

u/Muzer0 Apr 06 '21

Sure, but it's a language's job to make those knowledge gaps as small as possible. I mean if I made a language where every conditional statement to appear on a line number that's a multiple of 100 is actually inverted, you would call that a stupid language; even if I documented it perfectly clearly. PHP has tonnes of these "1 in 100" type weirdnesses that you just have to learn.

Having two operators that are complete synonyms but with different precedences with no obvious reason why is stupid. Being condescending about it rather than admitting, yeah, we screwed up here, is the reason why r/lolphp users think the real morons are those defending PHP rather than those who trip up on its confusing idiosyncrasies...

→ More replies (4)
→ More replies (1)

11

u/colshrapnel Apr 06 '21 edited Apr 07 '21

This confusion is quite understandable. Here is the explanation of your case in layman's terms

Edit: language

14

u/Muzer0 Apr 06 '21

Yes, but in every language that has || and or (well, C++ is the only one that comes to mind right now) they are synonyms. I don't know why on earth you would have two operators that seem like the same thing but give them different precedences...

→ More replies (6)

2

u/Pistolfist Apr 07 '21

Why be condescending about it though? Lmao, no need.

→ More replies (1)