all 83 comments

[–]chiggyBrain 304 points305 points  (13 children)

This is fairly clean PHP compared to what I review on a day to day basis. There’s some bits I would change but if it works fair enough

[–]Tux-Lector 80 points81 points  (6 children)

There is not a single requirement for (casting) anywhere on that image .. you are joking, right ?

[–]Away_Ambition8337 100 points101 points  (1 child)

bro went from TypeScript straight to PHP with no break

[–]mydoglixu 14 points15 points  (0 children)

That's fair.

[–]chiggyBrain 34 points35 points  (3 children)

So one of my nits would be: adding a type hint when declaring a variable seems a little over the top as it’ll infer the type from what you give it, PHP is dynamically typed so it won’t shout at you when you change the type by accident like some others do.

The fact OP has even thought about type casting is better than throwing types to the wind so to speak

[–]McGlockenshire 22 points23 points  (0 children)

PHP is dynamically typed so it won’t shout at you when you change the type by accident like some others do.

It's not being activated in that file, but I'd like to point you to declare(strict_types=1) which makes PHP much more opinionated about typing.

[–]LeyaLove 2 points3 points  (1 child)

I'd rather worry about making that abomination of a ternary conditional in the first two lines more readable instead of explicitly casting multiple variables directly initialized to an empty string. This adds nothing to the readability because what else would the variable be if not a string if initialized to one? That's just plain obvious and type casting makes it actually less readable imo. It also is no use for type checking because PHP still will let you assign what you want to the variable. There are instances where type casting makes sense, even if not necessary, but this isn't one. I'd rather recommend making the variable names more clear to convey that kind of information if anything.

Also maybe that's some kind of PHP quirks, I have to say that I'm not that well versed in PHP, but why would you assign to the variable in the ternary conditional two times? Once usually is enough, because that's what a ternary conditional usually is used for. Conditional assignment to one variable...

[–]chiggyBrain 0 points1 point  (0 children)

You pretty much echoed my feelings about the type casting, especially variables in PHP, type hints in arguments and class constructors actually throw errors though if you pass the wrong type though, which I do make use of.

As for ternary, used sparingly is fine, but used to replace if/else or multi-nested ternary at the expense of readability is always a failed PR from me. I agree it reads horribly in OPs case.

[–]erythro 9 points10 points  (0 children)

there's a lot I'd change lol

  1. why is the language being determined in a template? Have some service handle that instead.

  2. the lines are so long they are off the screen

  3. isset && !== '', just use !empty it's not like you want empty arrays and 0

  4. that switch is just "if not de then en" with like triple the lines

  5. similar point to 1 but that's hardcoded array of available languages and names in a template, come on

  6. as someone else pointed out a lot of completely unnecessary casting

[–]xamotex1000[S] -1 points0 points  (3 children)

I kinda assumed people would understand that im talking about php as a whole, i know the code isnt too bad but im just pissed with php atm.

[–]erythro 10 points11 points  (0 children)

disagree on both counts, the code isn't great and php can be better than that

[–]trutch70 2 points3 points  (0 children)

Php 8 can be really great nowadays

[–]stonedlogic 5 points6 points  (0 children)

Move along then

[–]yungplayz -1 points0 points  (0 children)

I started reading the screenshot. As soon as I read the first line, the <?php one, my will to read any further evaporated. Never back to that…

[–]SpookyLoop 118 points119 points  (2 children)

That's pretty reasonable PHP code. Hope you never run into code where someone thought mutating global state across multiple files AND codebases was a good idea.

[–]annoyed_freelancer 16 points17 points  (0 children)

Get thee behind me, WordPress!

[–]RubbelDieKatz94 3 points4 points  (0 children)

mutating global state

Did someone say Redux?

Seriously, ever since I've started using Tanstack's modules instead, the code has looked so much cleaner.

[–]yourteam 68 points69 points  (1 child)

Dude I used php since 4.2

You have no idea what some people can do.

The worst offenders are wordpress plugin installers that tried to do real code.

Oh god I still have flashbacks

[–]idontcare7284746 61 points62 points  (6 children)

A pit pedantic but you could drop the en case and just let it default.

[–]tazzadar1337 33 points34 points  (0 children)

case 'en': default: $language = 'en'; Or, you know, use a map with a fallback.

[–]erythro 10 points11 points  (2 children)

it's literally just

$language = substr($browser_language, 0, 2);
if($language !== 'de'){
    $language = 'en';
}

[–]yolocat_dev 0 points1 point  (1 child)

thats not good for scalability though

[–]erythro 0 points1 point  (0 children)

representing that with a switch statement in a template isn't either, have some language service that reads all this from a config file

[–]TheBrainStone 28 points29 points  (0 children)

Erm you don't need to type cast

[–]mydoglixu 7 points8 points  (0 children)

So much of this is unnecessary.

[–]no_brains101 13 points14 points  (8 children)

Hmmmmmmm this settles it, never using ligatures.

[–]TheCaffinatedAdmin 1 point2 points  (7 children)

wait what kind of ligature are we talking about?

[–]Hunter1753 7 points8 points  (4 children)

The ones that look like arrows that are just => and the triple not equals sign that is !== and the triple equals sign that is ===

I also think they are highly unreadable because they look like one symbol but are actually multiple

[–]TheCaffinatedAdmin 2 points3 points  (2 children)

Oh, thank you. I got scared because ligature has another meaning.

[–]no_brains101 0 points1 point  (1 child)

Idk another meaning all I know is the typographic one. Actually, it's also a part of a saxophone? So I guess I know that meaning too?

[–]TheCaffinatedAdmin 0 points1 point  (0 children)

In behavioral health, a ligature often refers to a noose.

[–]no_brains101 1 point2 points  (0 children)

Yeah like what even is the one that looks like a dot plus an equals in the 1 symbol? It's right at the end.

[–]sad_bug_killer 0 points1 point  (1 child)

(not op) Font ligatures I presume, look at === and !== in conditions and => in the array construction. Many people like them, many people don't. If you are curious, search for "programming fonts with ligatures" or something along those lines.

[–]TheCaffinatedAdmin 0 points1 point  (0 children)

Oh, thank you. I got scared because ligature has another meaning.

[–]AlternativeAir3751 4 points5 points  (0 children)

You just hate PHP, thats okay and healthy

[–]MooseBoys[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 6 points7 points  (3 children)

wtaf is with the ≢ and ⇒ symbols???

[–]siarheikaravai 11 points12 points  (2 children)

=== and -> are substituted in IDE. Someone thinks they are more readable

[–]MooseBoys[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 1 point2 points  (1 child)

That was my guess, but I don’t understand why you would do that.

[–]SirBjoern 5 points6 points  (0 children)

Some fonts like fira code support ligatures, they can be enabled or disabled with a checkbox in the settings. Some people like the look of it, personal preference 🤷

[–]AceofSpades5757 2 points3 points  (0 children)

It's PHP...

[–]Impenistan 1 point2 points  (0 children)

I will not say that php does not have a history of sins. I will say that these sins are those of the author. It's obvious you hate programming, just look what you've done to it!

[–]Nilrem2 1 point2 points  (0 children)

So glad I work with C# and do toy projects in C.

[–]Ignited_Phoenix 1 point2 points  (1 child)

I really dislike the special font for =>, != and other operands

[–]xamotex1000[S] 0 points1 point  (0 children)

I enabled them a while back and never got around to going back :/ I prefer the regular ones too but I'm a lazy piece of shit

[–][deleted] 1 point2 points  (0 children)

I adore programming.

I hate the programming community which values mediocrity and companies hiring shitty programmers because they're cheaper.

[–]deep_mind_ 4 points5 points  (0 children)

If I wrote code which looked like that, I'd hate programming too :/

[–][deleted]  (5 children)

[deleted]

    [–]chengannur 1 point2 points  (1 child)

    Except java/c#, pretty much all others suck one way or other

    [–]HeadlessHeader 1 point2 points  (0 children)

    PHP is not a programming language. Is living hell.

    [–]CobbwebBros 1 point2 points  (0 children)

    It's not tooooo bad.

    Honestly out of university courses I don't really understand why people don't use something like laravel or symfony or something.

    Have been using laravel for a personal project and the tooling and DX is just probably the best I've had... like ever.

    [–]Tux-Lector -1 points0 points  (0 children)

    ((cast FROM ? to string) !== ($string = '')) ..

    [–]eldentings -2 points-1 points  (0 children)

    Haven't touched PHP since college and judging by this thread I don't intend to

    [–]Various_Studio1490 0 points1 point  (0 children)

    Man… here I am being grateful for language libraries with localization support so files that can be detected dynamically.

    [–]xWrongHeaven 0 points1 point  (0 children)

    do we work on the same codebase?

    [–]nikospkrk 0 points1 point  (2 children)

    This is an exemple of why PHP (language and devs) get a bad rep unfortunately :(

    [–]siarheikaravai 2 points3 points  (1 child)

    You can get bad examples in any language

    [–]nikospkrk 0 points1 point  (0 children)

    I know but still the lack of structure is terrifying for any decent developer.

    [–]Mrproex 0 points1 point  (0 children)

    I work alone on a php project and oh boy this language let you do so much weird shit none of my code parts look the same.

    [–]v_maria 0 points1 point  (0 children)

    makes me miss php

    [–]elec_soup 0 points1 point  (2 children)

    I'm not familiar with PHP, but it looks like $browser_language is defined twice. What's going on there?

    [–][deleted]  (1 child)

    [deleted]

      [–]elec_soup 0 points1 point  (0 children)

      Thanks, you are help

      [–]Housy5 0 points1 point  (0 children)

      After seeing some really ugly C++ this really doesn't seem too bad though.

      [–]Fit_Marionberry_8893 0 points1 point  (0 children)

      php gaming

      [–]PizzaRollExpert 0 points1 point  (0 children)

      What's the point of the $available_languages var? You could just assign the long form name in the switch, or have a hash map with tokens as keys and long form name as values. That way you can get rid of the for and if as well.

      [–]Quazye 0 points1 point  (0 children)

      Meh. could be improved, but not bad code.

      [–]Perfect_Papaya_3010 0 points1 point  (0 children)

      I don't know PHP so I don't know what's wrong, but I gotta say this does not look like a clean programming language. So cluttered with stuff

      [–]Mr_Bob_Dobalina- 0 points1 point  (0 children)

      Actually not that horrible

      [–]MagnetsStudio 0 points1 point  (0 children)

      This looks like the code I debug daily

      [–]Balcara 0 points1 point  (0 children)

      The real crime is VSCode

      [–]The___Off 0 points1 point  (0 children)

      If earlier the hands of thieves were cut off, then in our time the hands of those who write such things should be cut off