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, (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

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

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

A small trick, combining named parameters, spread and union arrays operators to « easily » create a modified copy of a DTO: https://3v4l.org/ZWX5G#v8.2.10

⚠️ It’s fun if you have a lot of parameters, but using a string containing the parameter’s name isn’t really satisfactory 😕

dseguy,
@dseguy@phpc.social avatar

@b_viguier You can extend this syntax to 8.0+ with a clever array_values() / array_merge() in that syntax.

https://3v4l.org/igrsW

$copy = new DTO(...(array_values(array_merge(get_object_vars($dto), ['d' => 43]))));

Now, this extended syntax is an easy prey to property definition order, constructor argument order, and temporary property deletion, unlike your original approach.

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

Customs exceptions are classes like any others: they may implements an interface.

That interface may be used to catch the exception, even if the interface has nothing to do with exceptions.

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

I just watched a conference, where the speaker complained that, for no good reason, concatenation operator is comma ','

This is definitely not the most popular concatenation operator, yet it kinda happens in one situation. Can you name it?

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, to php French
@dseguy@phpc.social avatar

I always wondered why allows to NOT a variable on the LEFT side of an assignation.

It makes sense with an iffectation (an assignation in an if)

I'm sure other such expressions are possible, with unary operators.

Definitely not for me, for readability reasons; same as !$o instanceof X.

https://3v4l.org/3tfWb

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

Here is a case where the difference between a constant and a name (class, function, method...) is fuzzy.

There is, at least, another case. Which one?

https://3v4l.org/fMmaU

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

If you forgot in which PHP version a feature was introduced, https://caniphp.com/ did not forget so you can check it there.

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

With 8.1, it is possible to add (...) (parenthesis, three dots, parenthesis) as you like, between the method name and the arguments. This is fun!

The last command is also possible, but requires a bit of brain twisting. How to make it work?

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

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

TIL that also supports attributes with enum, interfaces, traits and enum's cases.

https://3v4l.org/VO82i

https://kirschbaumdevelopment.com/insights/extending-php-enums-with-attributes

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

Sunday puzzle :

How can you make foreach() emits several times the same key ?

Multiple solutions possible : please, submit one only.

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