5 ms·
PHP is a language with various features that might make it attractive to certain people: • CGI-like execution model when used for the web, making resource le
by TazeTSchnitzel 5y ago
PHP is a language with various features that might make it attractive to certain people:
• CGI-like execution model when used for the web, making resource leaks very difficult and encouraging scalable design
• not only integers and floats, but also strings, lists and dictionaries as mutable value types (copy-on-write) — this is a big difference from JavaScript and Python
• unusually for a dynamic “scripting” language: true classes and interfaces, and optional type declarations enforced by the runtime
• a standard library that doesn't require import statements!
• (a very new feature:) fibers, a way do asynchronous execution without every function in the callstack having to be aware of it
• binary-safe strings without presumptions from the language about what encoding is in use
• a very nice package manager
• a highly optimised runtime
But its history has made it a quite messy language which can be frustrating to learn, and which will always carry a higher mental burden than something like Python. It is a shame.
- babuskov 5y ago* duct-tape style of arrays that Just Work(tm) as arrays, hash maps, etc. without having to use any boilerplate code. It's the main reason why I still prefer PHP for quick&dirty scripting and data processing.
- Navarr 5y agoI love PHP "arrays" but there is one tiny gotcha that becomes more important as we do type-safety stuff more and more. `$a['1'] = 'string';` becomes `$a[1] = 'string';`. This appears to be the case for any fully-fledged decimal number where `(string)(int)$key === $key` So if you are expecting `array_keys` to return all strings, you might get surprised
- dotancohen 5y agoSome years ago I was appending underscores to key strings to avoid that. We're talking PHP 5 era. But even at that time stdClass was available. Today, I much prefer either using a stdClass object or actually making a real class when I reach for an associative array. They still have their use cases, though.
- diarrhea 5y ago> not only integers and floats, but also strings, lists and dictionaries as mutable value types (copy-on-write) Lists and dictionaries are also mutable in Python. Strings are not. With CoW, are your referring to garbage collection? (value that is no longer assigned to a name will eventually be purged). That would be independent of (im)mutability, so I'm not sure I understand.
- withinboredom 5y agoWith copy-on-write: $a = ['a']; $b = $a; $a and $b both point to the exact same bit of memory. Thus passing an array between functions is zero-cost. When you do: $b[] = 'b'; // append 'b' a new array is allocated, the old data copied to it, and 'b' appended to it (basically, but IIRC, it is a bit more optimized than that).
- deleted 5y ago[deleted]
- kaba0 5y agoSo basically what FP languages do?
- withinboredom 5y agoSurprising php has a lot of FP aspects.
- pas 5y agoIt .. really doesn't. (Sure, probably a tiny bit more than Python.) It's not static types FP like Haskell/Scala (or even TS), as it just recently started gaining some type safety, and while it had map/reduce call_user_func, it wasn't that big in PHP world. It simply has/had too much mutability for it, and most PHP practice focused on trying to implement OOP ideas in it in a nice way.
- 5y ago
- withinboredom 5y agoI don't think fibers are that great. There's no indication that a function call will create a fiber and do things[1] that might screw with your current state. When you know fibers are around, you need to code more defensively. [1]: https://withinboredom.info/blog/2022/01/04/thoughts-on-php-fibers/ https://withinboredom.info/blog/2022/01/04/thoughts-on-php-f...
- eyelidlessness 5y ago> a standard library that doesn't require import statements I consider this a non-feature in JS. The global namespace is enormous and growing rapidly. As I recall, PHP’s global namespace is similarly enormous (and notorious for its inconsistencies and redundancies). I understand that there are ergonomic advantages to not having to import built-ins, but I think framing it as a feature deserves a healthy asterisk :) > fibers, a way do asynchronous execution without every function in the callstack having to be aware of it This isn’t built into JS, but there’s an implementation of it, mostly used and popularized by Meteor. Again it has some ergonomic benefits, but I think it can too easily hide/obscure deoptimizations—eg performing IO operations in sequence which would be better performed concurrently.
- asddubs 5y agowell, PHP does have proper namespace support so you can easily avoid collisions. although I don't really think it's that much of a problem either way
- kyriakos 5y agoI think JavaScript global namespace is massive in the browser. Under nodejs things are much saner.
- eyelidlessness 5y agoIt’s increasingly almost the same, aside from very DOM/*ML rendering contexts, and getting more similar as time goes on. Which is good for portability but JS (ECMAScript/TC-39) is huge in itself, and the various compatible things in the JS runtime ecosystem (browsers/Node/Deno) is also huge. Node was historically more limited here but given how much the language itself is expanding they have to choose compat.