Advertisement ยท 728 ร— 90
#
Hashtag
#phptip
Advertisement ยท 728 ร— 90
Post image

#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...

1 1 0 0

#phptip #phptrick

0 0 0 0
Post image

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...

0 0 0 0
Post image

#PHP defined() in action: there are many aspects when checking for a constant existence.

#phptip #phptrick

php-tips.readthedocs.io/en/latest/ti...

0 0 0 0
Post image

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...

0 0 0 0
Preview
ReturnTypeWillChange Is For All ``ReturnTypeWillChange`` is an attribute that tells PHP that the return type of the related method is different from the defined by the PHP native methods

#[ReturnTypeWillChange] works on native #PHP methods and also on custom ones.

#phptrick #phptip

php-tips.readthedocs.io/en/latest/ti...

0 0 0 0
Post image

#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...

0 0 0 0
Preview
::parent Operator PHP provides the ``X::class`` operator to access a class's fully qualified name

A ::parent operator in #php?

#phptip #phptrick

php-tips.readthedocs.io/en/latest/ti...

0 0 0 0
<?php

const from = ['yield from'];

$a = fn () => yield yield from from;

foreach($a() as $b) {
    print $b;
}

<?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...

2 0 0 0
<?php 
class X { 
    private array $code = []; 
    
    function foo() { 
        return (string) $this<-code; 
    } 
} 

var_dump((new X)->foo());

<?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

0 0 1 0
<?php

class x {
   const int|string A = 1, B = 'abc';
}

echo strlen(X::B);

<?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...

0 0 0 0
Post image

#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...

0 0 0 0
<?php

echo 'B', 'A', sqrt(-1), 'A'; 

?>

<?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...

2 1 1 0
<?php

$string = '123a';
$integer = -$string; // warning!

<?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...

3 0 1 0
<?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]);

<?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...

1 0 0 0
Post image

Closure, constant, #PHP 8.5, parenthesis, ants and cast: what could go wrong, right?

php-tips.readthedocs.io/en/latest/ti...

#phptip #phptrick

1 0 0 0
Post image

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...

0 0 0 0
Post image

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...

0 0 1 0
Preview
Convert a string to integer with PHP - Exakat How to Convert a string to integer with PHP: since PHP 8.5, there is a warning for larger than integer strings, so how do you do it?

Converting a string to an int, without a warning.

A PHP 8.5 challenge.

#phptip #phptrick

www.exakat.io/convert-a-st...

1 0 0 0
<?php

class X {
  // Some magic happen here
}

X::foo();
(new x)->foo();

<?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...

1 0 0 0
<?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

<?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

2 0 0 0
Post image

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...

1 1 0 0
Post image

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...

3 0 1 0
Post image

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...

0 0 0 0
Post image

#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...

1 0 0 0
Post image

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...

0 0 1 0
Post image

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...

0 0 0 0
Post image

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...

0 0 0 0
Post image

It is Friday, so why not cram as many #PHP operators in one single operation, and make it work?

Look at this beauty! Could you make it run?

#phptip #phptrick

php-tips.readthedocs.io/en/latest/ti...

1 0 0 0
Post image

A rare find: A syntax that worked before #PHP 8.1 and after #PHP 8.2 but not in #PHP 8.1

Here is the never arrow function.

#phptip #phptrick

php-tips.readthedocs.io/en/latest/ti...

1 0 0 0