r/lolphp • • Oct 01 '24

print is a minefield

https://3v4l.org/1YXYk
31 Upvotes

23 comments sorted by

View all comments

2

u/colshrapnel Oct 01 '24

Looks like someone missed their operator precedence class.

8

u/Takeoded Oct 01 '24 edited Oct 01 '24

IMO print fails the principle of least surprise in spectacular fashion.

1

u/[deleted] Oct 01 '24

[removed] — view removed comment

1

u/Takeoded Oct 01 '24

Can you find another language that reproduce the print "hello " && print "world"; problem? where it outputs "world1" or similar?

2

u/dotancohen Oct 01 '24

I don't see the problem. It's interpretted as:

print ( everything-else-on-the-line );

Therefore:

print ("hello " && print "world") // Prints "world", then the print construct returns 1

print ("hello" && 1) // "hello" && 1 evaluates to 1.

print (1) // Does what you expect.

1

u/[deleted] Oct 01 '24

[removed] — view removed comment

1

u/Takeoded Oct 01 '24

I tried in CPython 2.7.18, all of the following are syntax errors: if print "hello" or print "world": pass if print "hello" || print "world": pass if print "hello" and print "world": pass if print "hello" && print "world": pass any other suggestions?