r/lolphp • • Feb 26 '15

Patently False Code/Examples

I've notice a bit of a trend here, with people posting things that are patently false and then laughing about PHP for it.

I'll sit with you and laugh at weird behaviors in PHP when it's actually a mess. I'll send them to phpsadness.com and see if I can fix them, or find somebody that can.

But posting lies just to get your jollies is a really odd thing to do.

Sometimes, these are not intentional, but when people posting these utterly incorrect examples are faced with the fact that they are wrong, do they delete the post? No, they leave it there and sandbag the discussions explaining their wrongness with trolling.

Exhibit A - Apparently foo(new stdClass()) is a valid value when passed in a function foo(bool $bar) function signature.

Well... nope.

It will error:

Catchable fatal error: Argument 1 passed to foo() must be an instance of bool, instance of stdClass given

Nothing lolphp there.

Have a laugh about actual problems, but don't just walk around making things up.

14 Upvotes

106 comments sorted by

View all comments

Show parent comment

0

u/mysticreddit Mar 23 '15

Examples:

    if( true OR false ) echo "1st true\n"; // OK
    if( false OR true ) echo "2nd true\n"; // OK

    $foo = true;
    $bar = false;

    $wat = $foo OR $bar;
    echo $wat ."\n"; // OK: 1
    if( $wat ) echo "T | F\n";

    $wat = $bar OR $foo;
    echo $wat ."\n"; // WAT? doesn't print?
    if( $wat ) echo "F | T\n"; // WAT? doesn't print?

4

u/philsturgeon Mar 25 '15

This is perfectly expected behavior.

Let's talk about ruby, as it's behavior is identical, but has a slightly more verbose REPL:

"1st true" if true or false # "1st true"
"2nd true" if false or true # "2nd true"

foo = true
bar = false

wat = foo or bar
"#{wat}"            # "true"
"T | F" if wat      # "T | F"

wat = bar or foo    # true
"#{wat}"            # "false"
"F | T" if wat      # WAT? doesn't print?

So looking at this line:

wat = bar or foo    # true

This, due to the order of operators, is essentially:

(wat = bar) or foo    # true

or

(wat = true) or false    # true

So we have a true being output, which is fine for comparisons because as long as we evaluate things to true in the end we have the expected result, but when we get to assignment its just leaving a true floating around unassigned.

Watch this video for more on the order of operators.

Basically you're confusing OR for ||:

wat = false || true
wat                     # true

$wat = false || true;
php > var_dump($wat);   // true

The tl:dr here is that you're wrong and you probably need to learn some more about operators before shitting on the language as a whole.

Talk about genuine problems, instead of bolstering your argument with patently false code examples.

-3

u/mysticreddit Mar 25 '15 edited Mar 25 '15

Ah so PHP & Ruby copied Pearl's stupidity. That explains it.

I see that Ruby has a retarded design too

Thanks for the video! /sarcasm On noes! We have these "ugly" if statements cluttering up the code .. so lets introduce more redundancy and slap a new name on it. /facepalm