Live data from Hacker News

PHP 8

php.net

101–110 of 273 posts

Re: PHP 8

#102
post #28

I find it really weird that the match expression is strict comparison, but everywhere else it's loose comparison. Don't get me wrong, I prefer strict comparison, and prefer strongly typed languages because of it, I just feel that this is a strange design choice. In addition, the @ operator seems to have functionality changed, and there is plenty of other backwards incompatible changes to go along with it, and yet, on…

Not a PHP dev, but I read a lot of it for work: based on the example provided, I gather the PHP community accepts that the non-strict behavior of ‘switch’ was undesirable, but it would be too difficult to break the non-strict semantics in an existing construct.

Likewise, the fact that ‘match’ is an expression rather than a statement encourages stricter semantics: although it’s technically unrelated to the type coercion issue, ‘match’ expressions should be easy to reason about locally, which isn’t the case for switch statements. This idea is encouraged by the fact that ‘match’ branches can’t continue across multiple lines: although technically ‘match’ could mutate some complicated global state, the intended use case is a constant or a simple function - easy to understand, and relatively gotcha-free. Gotchas should go in a ‘switch’ statement.

Seasoned PHP devs aren’t going to accidentally use the brand-new ‘match’ statement without careful study, and I don’t think the inconsistency is too big of a deal for newer devs: the overall principle is that ‘match’ is safer than ‘switch’ but less powerful.

Re: PHP 8

#103
post #52

Earlier quoted context omitted.

but why all of a sudden languages started to add this? Maybe the function itself needs to be simplified, maybe there is a design issue. i am failing to see how "if else" can be simplified via a synth sugar.

It's a switch statement that's an expression . That's a nice ergonomic improvement when you want to assign the result to a variable. Especially as it allows you to use a non-mutable variable whereas otherwise you'd have to make it mutable.

I wouldn't compare it to a switch statement, because a switch statement - even though it looks similar - has fundamentally different control flow.

Re: PHP 8

#104
post #86

Finally, after all these years, Named Arguments! https://stitcher.io/blog/php-8-named-arguments

I don't understand why pretty much no language does that these days. I think the argument is that your IDE should do this, and it does in some cases, but still...

You can get a much more powerful alternative for "free" if you support de-structuring assignments (in arguments).

JavaScript Example:

    function test({a,b,c}) {
        console.log(c,b,a)
    };
    
    test({
        a: 1,
        b: 2,
        c: 3
    });
If you have this there's really no need for named arguments anymore. If you add features to your de-structuring, your "named arguments" will have them too (things missing for JS specifically: optional objects, TS support to specify types for the destructured objects inline instead of repeating the entire thing).

Re: PHP 8

#105

I got spoiled by PHP as my first language. It was so easy to get going. I was surprised to learn that other languages didn't work the same way, when integrating into a web server. Also it has always been rock solid. A bug in my code brought down only that request for that user, totally did not affect or even slow down responses to other users. People have attacked its syntax forever. I avoided the worst of it by lear…

REMOTE_USER as in HTTP Basic auth? the ugly login prompt you can not logout of again?

In my app, I use basic auth in place of captcha.

Every useragent going back to IE3 and NN1 support it.

I then set a cookie, and users never see the prompt again.

Cookies are sent on the first request, before auth happens.

In my extensive testing, even the most "non-techy" people are able to complete this task, while most bots fail at it.

If any bots start getting through, I can change the password.

For visitors I haven't met yet, I can give out guest credentials on the 401 page.

Re: PHP 8

#106
post #8
post #4

Apparently some of the deprecations will make this the most backward compatibility breaking version jump of PHP since V4 to V5. (Read a german interview here: https://www.heise.de/news/PHP-8-im-Experten-Check-Der-erwart... ) There's a long report on Wordpress and PHP 8 and it's not pretty: https://developer.yoast.com/blog/the-2020-wordpress-and-php-...

> There's a long report on Wordpress and PHP 8 and it's not pretty This is an awkward side-effect of WordPress core team largely neglecting PHP code quality issues for the last decade or so. With good reason – improving such a large codebase is a massive, mostly-thankless task.

I worked at WordPress for half a year, and I'm no expert, but the code reviews there were more intense than any I've ever experienced. Every line of code gets reviewed before it gets committed, and if the commit is longer than 20 lines or so, it undergoes an additional scheduled review as well as pre-commit review. They also have pretty good code sniffers that ought to catch errors like OP mentioned.

The reason they're concerned, I think, is because they try to protect users who have plugins that came from their plugin repository, where the same code quality rules don't apply. WordPress has always been huge on backward compatibility.

Re: PHP 8

#107

How should one start learning with PHP these days? My experience: hacked together a few PHP scripts over the years to notify me of a website change (5 minute cron job). Before that, I was a very good beginner with VB6 back in highschool. There are a couple of ambitious database-driven website projects I would like to create, but I don't know where to start. I like the KISS philosophy, and I think PHP and MySQL would…

It hasn't been updated since April 2020 but this website used to be a good resource for "modern PHP":

https://phptherightway.com/

Re: PHP 8

#108
post #91

0 == 'foobar' // false > When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string comparison. Why do the conversion at all? Why couldn't this just be an error? I realise there is also `===` the strict comparison that typechecks arguments, but why not make that the default?

> Why couldn't this just be an error? you mean crash your program? So your user finds a way to send you an int instead of a string, or vice versa, and now they can crash your webserver.

technically they'd just crash that one php process, not the whole web server. :)

I think the bigger 'why not' is ... 25 years of backwards compatibility.

Re: PHP 8

#109
post #88
post #46

Earlier quoted context omitted.

Curious if that's a general aversion to dynamic typing, or specifically PHP.

The problems with PHP aren't really with the concept of dynamic typing as such, but rather with the somewhat curious semantics and really awkward standard library. These are relics from the past that are, unfortunately, very hard to fix without a "Python 3000 moment", and all the problems that brings. Although PHP 8 does fix a bit of it (various comparisons and operators are a bit better now) there's still a lot of i…

They're a bit overblown now I feel, and I wouldn't throw out the language and ecosystem just because a comparator got fussy. You probably won't win any "Code is Art" contests, and if you want to do computer science work then why even waste the time hating PHP, it was never for that. PHP is a web application workhorse, and it's really exceedingly good at that task.

Re: PHP 8

#110
post #100

There have been 5 major threads about PHP 8 in the last few months: https://news.ycombinator.com/item?id=24866190 https://news.ycombinator.com/item?id=24235440 https://news.ycombinator.com/item?id=24320024 https://news.ycombinator.com/item?id=24150731 https://news.ycombinator.com/item?id=23955197 A couple of those are more technically specific (to JITs and named arguments) but the vast majority are just the usual com…

Thanks for the explanation – I was wondering why it jumped from the top spot.

This particular post might want to get a reprieve – not because the discussion is interesting, but because the news itself – PHP 8.0.0 being released – is reasonably important.

Post reply on HN