#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/latest/ti...
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/latest/ti...
#PHP defined() in action: there are many aspects when checking for a constant existence.
#phptip #phptrick
php-tips.readthedocs.io/en/latest/ti...
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/latest/ti...
#[ReturnTypeWillChange] works on native #PHP methods and also on custom ones.
#phptrick #phptip
php-tips.readthedocs.io/en/latest/ti...
#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/latest/ti...
<?php const from = ['yield from']; $a = fn () => yield yield from from; foreach($a() as $b) { print $b; }
OK, I'm tired and this #PHP make me chuckle.
#phptip #phptrick
php-tips.readthedocs.io/en/latest/ti...
<?php class X { private array $code = []; function foo() { return (string) $this<-code; } } var_dump((new X)->foo());
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
<?php class x { const int|string A = 1, B = 'abc'; } echo strlen(X::B);
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/latest/ti...
#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/latest/ti...
<?php echo 'B', 'A', sqrt(-1), 'A'; ?>
What do you think this #PHP code will write?
Could we do better ?
#phptip #phptrick
cc @lecodeestdanslepre.fr ๐
php-tips.readthedocs.io/en/latest/ti...
<?php $string = '123a'; $integer = -$string; // warning!
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/latest/ti...
<?php function foo() { print_r(func_get_args()); } $array = [1,2,3]; // Not possible // cannot use positional argument after unpacking //foo(...$array, ...$array, 4); foo(...$array, ...$array, ...[4]);
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/latest/ti...
Closure, constant, #PHP 8.5, parenthesis, ants and cast: what could go wrong, right?
php-tips.readthedocs.io/en/latest/ti...
#phptip #phptrick
TIL that it is possible to use cast, like (int) or (string) with constants, since #PHP 8.5.
#phptip #phptrick
php-tips.readthedocs.io/en/latest/ti...
The global keyword imports variables from the outside, and overwrites the local value. Beware, when using it.
#phptip #phptrick
php-tips.readthedocs.io/en/latest/ti...
Converting a string to an int, without a warning.
A PHP 8.5 challenge.
#phptip #phptrick
www.exakat.io/convert-a-st...
<?php class X { // Some magic happen here } X::foo(); (new x)->foo();
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/latest/ti...
<?php echo intval('1AZ') .PHP_EOL; // 1 echo intval('1AZ', 11).PHP_EOL; // 21 echo intval('1AZ', 21).PHP_EOL; // 31 echo intval('1AZ', 36).PHP_EOL; // 1691 echo intval('1AZ', 37).PHP_EOL; // 0 echo intval( 123, 11).PHP_EOL; // 123 echo intval('123', 11).PHP_EOL; // 246
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/latest/ti...
#phptip #phptrick
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/latest/ti...
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/latest/ti...
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/latest/ti...
#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/latest/ti...
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/latest/ti...
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/latest/ti...
When using #PHP isset(), the language construct hides errors because, that's what is being tested there.
Unless there is an object, where an array is expected. Then, it leads to a fatal error.
It prevents the usage of an optimisation.
#phptip #phptrick
php-tips.readthedocs.io/en/latest/ti...