#phptrick — Public Fediverse posts
Live and recent posts from across the Fediverse tagged #phptrick, aggregated by home.social.
-
#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.
https://php-tips.readthedocs.io/en/latest/tips/not_set_but_exists.html
-
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.
https://php-tips.readthedocs.io/en/latest/tips/static_arrow_function.html
-
#PHP defined() in action: there are many aspects when checking for a constant existence.
-
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!
https://php-tips.readthedocs.io/en/latest/tips/preg_split.html
-
#[ReturnTypeWillChange] works on native #PHP methods and also on custom ones.
https://php-tips.readthedocs.io/en/latest/tips/returntypewillchange_for_all.html
-
#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.
https://php-tips.readthedocs.io/en/latest/tips/compare_nan.html
-
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() ?
https://php-tips.readthedocs.io/en/latest/tips/array_unique_and_enum.html
-
#PHP puzzle of the day: can you make Chidren::foo() call GrandParents::foo() ?
Think about it, and then, click below for the solution(s).
-
Would it be cool to have a #PHP ::parent operator, like we have the ::class operator
Well, turns out we can.
https://php-tips.readthedocs.io/en/latest/tips/parent_operator.html
-
OK, I'm tired and this #PHP make me chuckle.
https://php-tips.readthedocs.io/en/latest/tips/stuttering.html
-
So, #PHP typed class constants are vaguely useful?
Here is a nice usage case that will definitely makes everyone love them.
https://php-tips.readthedocs.io/en/latest/tips/crazy_const_type.html
-
#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
https://php-tips.readthedocs.io/en/latest/tips/hexadecimal_separator.html
-
What do you think this #PHP code will write?
Could we do better ?https://php-tips.readthedocs.io/en/latest/tips/errorNumber.html
-
What if you want to convert a #PHP string to the opposite number ? Can you cast (int) and use minus?
Well, can you?
https://php-tips.readthedocs.io/en/latest/tips/put_the_cast_where.html
-
So, ...[4] means unpacking a single value from a #PHP array.
This basically means reading the value itself.Unless it is useful sometimes...
https://php-tips.readthedocs.io/en/latest/tips/unpacking_single_array.html
-
Closure, constant, #PHP 8.5, parenthesis, ants and cast: what could go wrong, right?
https://php-tips.readthedocs.io/en/latest/tips/int_and_ant.html
-
TIL that it is possible to use cast, like (int) or (string) with constants, since #PHP 8.5.
https://php-tips.readthedocs.io/en/latest/tips/cast_in_const.html
-
The global keyword imports variables from the outside, and overwrites the local value. Beware, when using it.
https://php-tips.readthedocs.io/en/latest/tips/global_overwrite.html
-
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?https://php-tips.readthedocs.io/en/latest/tips/static_and_not_static.html
-
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!
-
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');
https://php-tips.readthedocs.io/en/latest/tips/easy_map.html
-
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.
https://php-tips.readthedocs.io/en/latest/tips/casting_enum.html
-
Dynamic class constants is achieved with #PHP 8.3 class constant syntax, and the constant() function.
It also applies to enumeration cases.
https://php-tips.readthedocs.io/en/latest/tips/dynamic_enum_cases.html
-
#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!
https://php-tips.readthedocs.io/en/latest/tips/array-callaback-index.html
-
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.
https://php-tips.readthedocs.io/en/latest/tips/closureCreation.html
-
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.
https://php-tips.readthedocs.io/en/latest/tips/fatal_error_on_coalesce.html