dseguy, to php French
@dseguy@phpc.social avatar

This is a reminder that only use int and strings as keys in array. Null is a special case here.

dseguy, to php French
@dseguy@phpc.social avatar

I finally found an occurrence of this beauty :

$this->$this

It still looks like a typo in the original code, but with the help of __toString, it works. Also, strict_types doesn't help here.

https://php-tips.readthedocs.io/en/latest/tips/this_this.html and more tips!

dseguy, to random French
@dseguy@phpc.social avatar

Choose your team : condition in a loop, or array_filter() first ?

Make up your mind, and then you can read this new blog post.

The result will NOT surprise you.

https://www.exakat.io/en/array_filter-versus-loop-condition-checks/

dseguy, to php French
@dseguy@phpc.social avatar

😂 This made me chuckle... welcome to 8.3 !

First array is the type, the second is the name of the constant, with the relaxed keyword, and the last one is the value of the array.

dseguy, (edited ) to php
@dseguy@phpc.social avatar

What is this piece of code will display in 8.2 ?

<?php

enum x: string {
case B = 'b';
const C = 'c';
}

class x2 {
const E = x::B->value;
const F = x::C + [];
}

echo x2::E;
?>

dseguy, to php French
@dseguy@phpc.social avatar

I know there is no such a function as empty(), as it is a language construct.

8.0 brought an improvement to this, as this is now a Fatal Error, rather than a Parse Error.

But, that aside, come on...

https://3v4l.org/ML6cX

dseguy, to random French
@dseguy@phpc.social avatar

TIL file_put_contents() accepts an array as second argument, and will do an implode("", $array) on it when writing.

Who is using this feature?

cc /@Girgias

https://php-tips.readthedocs.io/en/latest/tips/file_put_array.html

dseguy, to php French
@dseguy@phpc.social avatar

TIL you can get the following error message :

"Cannot add element to the array as the next element is already occupied"

How?

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

Since 7.4, there are numeric separators, to make numbers more readable.

They are only for hard-coded literals, so what do you do if you have stored them in a string ? 😇

https://www.php.net/manual/en/language.types.integer.php#language.types.integer.syntax

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

Friday Heart Attack :

What does this code will display ?

😜

dseguy, to php French
@dseguy@phpc.social avatar

allows to call any method statically, when within the same class. Here, self::foo() is a valid call (don't try this outside the same class...).

This is needed to call parent:: methods, without $this.

Question : what does $this contains in the method foo(), called statically ?
The object or null ?

dseguy, to php French
@dseguy@phpc.social avatar

Here is a very optional command : try-catch-finally

The finally clause in a try-catch-finally is optional: it can be omitted and often is.

The catch clauses in a try-catch-finally are also optional: they can be omitted.

When the catch and finally clauses are all omitted, the try clause can also be omitted. Safely.

https://php-tips.readthedocs.io/en/latest/tips/try-catch-finally.html

dseguy, to random French
@dseguy@phpc.social avatar

Could you improve an expression such as '$a == 'a' || $a == 'A'' ?

I collected 12 variations of that challenge.

in_array() is the surprising winner of that race, with strcasecmp() and isset() in tow. || (or) is not far and a good solution in any case.

https://www.exakat.io/en/unraveling-the-quest-for-the-fastest-case-insensitive-char-comparison-in-php/

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

Quizz question :

Can you write a function that won't execute when you call it?

(Just change the comment // more code )

dseguy, to php French
@dseguy@phpc.social avatar

A variadic argument collects all the unused named parameters, along with their key. That way, it is possible to handle them with their name inside the method.

On the other hand, array_merge (and some cousins) refuse them, and emits a Fatal error.

dseguy, to php French
@dseguy@phpc.social avatar

Cunning Little Switch : classic improvements to writing the old and simple switch in .

Some are errors, others are tips : all are worth keeping in mind.

https://www.exakat.io/en/well-structured-switch-command-in-php/

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

I think I found a 'void' parameter in . It is the second argument of array_keys().

That second parameter is often omitted (and unknown).

If present, it is typed 'mixed' to allow any value to be searched (here, null).

If absent, array_keys() returns ALL keys. When absent, it is not null, nor any other type. The last one possible is 'void'

Type is then : void|mixed.
Does that make sense or am I over-thinking this?

https://3v4l.org/VVDRW

dseguy, to php French
@dseguy@phpc.social avatar

TIL that has a dedicated function to calculate exp($x) - 1.

You can save quite some typing by using expm1($x);

It means exponential minus one.

https://3v4l.org/GCQX4

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

I'm still struggling to pick a side.

@ is too slow, because it merely hides the error.

?? looks dumb: it reads : if it is null, use null as default.

the if() command is long to type.

dseguy, to random French
@dseguy@phpc.social avatar

TIL that it is possible to redefine PHP functions and constants, using the 'use' command.

This is not the case for classes (or any CITE)

https://php-tips.readthedocs.io/en/latest/tips/redefine_native_functions.html

dseguy, to php French
@dseguy@phpc.social avatar

yield keyword allows anything as key, and that is transmitted to the calling foreach() command. So, you can have arrays or closures as keys, or worse.

Just a weird use case for tonight.

https://3v4l.org/o8jBb

dseguy, to php French
@dseguy@phpc.social avatar

What is a
function which has a return type, yet doesn't return anything?


dseguy, to php French
@dseguy@phpc.social avatar

One of my favorite math joke is to tell people that 'Some infinites are larger than others' (Which is true, see https://cantorsparadise.com/why-some-infinities-are-larger-than-others-fc26863b872f).

At the same time, in , infinite is accessible to anyone.

@fredbouchery

dseguy, to php French
@dseguy@phpc.social avatar

question of the day : when you know that
2*2 = 4
2^2 = 4 (too)
2.5^2.5 = 9.8821176880262..

Where is this 6 coming from ?

dseguy, to php French
@dseguy@phpc.social avatar

Useless tip of the day:

Null and booleans accept the array syntax, but always return NULL (and a warning)

Illegal types for array keys lead to a Fatal Error.

But not with NULL and booleans.

https://php-tips.readthedocs.io/en/latest/tips/nullAsArray.html

  • All
  • Subscribed
  • Moderated
  • Favorites
  • JUstTest
  • kavyap
  • Durango
  • InstantRegret
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • everett
  • Youngstown
  • normalnudes
  • slotface
  • ngwrru68w68
  • rosin
  • anitta
  • tacticalgear
  • osvaldo12
  • ethstaker
  • GTA5RPClips
  • cubers
  • Leos
  • tester
  • khanakhh
  • modclub
  • cisconetworking
  • provamag3
  • megavids
  • lostlight
  • All magazines