home.social

#phptrick — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #phptrick, aggregated by home.social.

  1. #PHP quiz of the day: how can a property be both not set, empty() and failing at comparison with falsy value?

    And more than one of us has this bug ATM.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  2. New word: zombie closure

    That is when you put $this in a static closure, you can run the #PHP code but you can't call it.

    Just a reminder about static arrow functions.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  3. preg_split() splits #PHP strings with regexes, captures the separators and can handle an empty regex.

    You don't need that everyday, but when you do, #PHP has it!

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  4. #PHP's NAN is 0 as an integer, 'NAN' as a string and not comparable with itself.

    Also, there are now warning when using them, so no one can get hurt by this.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  5. Enumerations are backed by strings, they work with in_array(), but not with array_unique()

    Well, at least, not directly.

    Also, did you know Enum::cases() ?

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  6. #PHP puzzle of the day: can you make Chidren::foo() call GrandParents::foo() ?

    Think about it, and then, click below for the solution(s).

    #phptip #phptrick

    exakat.io/calling-all-ancestor

  7. Would it be cool to have a #PHP ::parent operator, like we have the ::class operator

    Well, turns out we can.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  8. Can you make this #PHP riddle running?

    This compiles, so you can only add more code to make it work.

    I asked 5 AI, 2 succeeded, 3 failed.

    #phptip #phptrick

  9. So, #PHP typed class constants are vaguely useful?

    Here is a nice usage case that will definitely makes everyone love them.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  10. #PHP numeric separator works well on integer, floats and also hexadecimals, octals and ... binary. And most useful in the latter, right?

    0b1111_00_111_101_111

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  11. What if you want to convert a #PHP string to the opposite number ? Can you cast (int) and use minus?

    Well, can you?

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  12. So, ...[4] means unpacking a single value from a #PHP array.
    This basically means reading the value itself.

    Unless it is useful sometimes...

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  13. The global keyword imports variables from the outside, and overwrites the local value. Beware, when using it.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  14. Can a #PHP class have two methods with the same name?

    Not with signature overloading, a classic feature, right?
    But rather one method static and the other one non-static?

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  15. Do you think that intval() is a boring #PHP native function that turns string into integers?

    Well, it does, but it is not boring. Far from it!

    php-tips.readthedocs.io/en/lat

    #phptip #phptrick

  16. It is easy to create a map from a list of object, using #PHP array_column() and the second argument null.

    That null represents the whole object, and the third argument is the indexing key.

    array_column($list_of_objects, null, 'id');

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  17. It is not possible to cast any #PHP enum cases to (string) or (int) even when... they are backed!

    That looks like a natural feature, though.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  18. Dynamic class constants is achieved with #PHP 8.3 class constant syntax, and the constant() function.

    It also applies to enumeration cases.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  19. #PHP callable arrays are checked for 2 elements and only using index 0 and 1.

    No one said that 0 and 1 have to be in that order, and it works perfectly!

    Well done #PHP, well done!

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  20. There are, at least, 4 ways to create a closure in #PHP. They all will be callable later.

    On the other hand, they behave differently to check the underlying method.

    All is in the timing.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat

  21. A few days ago, I warned about isset() and a possible #PHP Fatal error.

    Given that isset() and ?? share the same code, it is not suprising that ?? hold the same trap.

    That bug should go away, the sooner the beter.

    #phptip #phptrick

    php-tips.readthedocs.io/en/lat