dseguy · @dseguy
82 followers · 117 posts · Server phpc.social

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.

3v4l.org/GCQX4

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
82 followers · 116 posts · Server phpc.social

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

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

@fredbouchery

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
79 followers · 112 posts · Server phpc.social

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?

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
73 followers · 106 posts · Server phpc.social

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 ? πŸ˜‡

php.net/manual/en/language.typ

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
67 followers · 90 posts · Server phpc.social

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?

3v4l.org/fMmaU

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
54 followers · 81 posts · Server phpc.social

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?

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
51 followers · 76 posts · Server phpc.social

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

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
49 followers · 70 posts · Server phpc.social

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 ?

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
48 followers · 68 posts · Server phpc.social

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

3v4l.org/VO82i

kirschbaumdevelopment.com/insi

#php #phptrick #phptip

Last updated 1 year ago

dseguy · @dseguy
41 followers · 49 posts · Server phpc.social

Today, I realized that the short assignment operators are the bitwise type, not the logical ones.

This is a 0, while I expected true.

There is not ||= nor &&= operator.

Could I ask for one in 8.3?

php.net/manual/en/language.ope

#php #phptip #phptrick

Last updated 1 year ago

dseguy · @dseguy
38 followers · 39 posts · Server phpc.social

Useless knowledge of the sunday afternoon :

'enum' is a valid name for enums and classes : legacy reasons.

Here, enum fails because the name is already taken by the class, even if defined later.

#php #phptip #phptrick

Last updated 1 year ago

· @dseguy
27 followers · 33 posts · Server phpc.social

foo() and goo() are both misusing generators.

Well, most probably, in any case.

#php #phptip #phptrick

Last updated 1 year ago

· @dseguy
21 followers · 21 posts · Server phpc.social

Sunday puzzle :

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

Multiple solutions possible : please, submit one only.

#php #phptip #phptrick

Last updated 1 year ago

Exakat · @Exakat
17 followers · 8 posts · Server phpc.social
Exakat · @Exakat
17 followers · 7 posts · Server phpc.social

Yesterday, I learnt that generators can also be used directly with the ellipsis operator.

Yield are collected in an array, and then spread.

This syntax ignores return statements. If you don't need the spread, use iterator_to_array().

#php #phptip #phptrick

Last updated 2 years ago

JΓ©rΓ©my DECOOL · @jdecool
146 followers · 441 posts · Server phpc.social

RT @Exakat

Avoid long list of /../../../ by leveraging the dirname() second argument.

php.net/dirname

#phptip #phptrick

Last updated 2 years ago

Exakat · @Exakat
16 followers · 6 posts · Server phpc.social

preg_match_all() is faster without the 3rd $matches argument, and then, it can be replaced by preg_match(), with another speed bump.

#php #phptip #phptrick

Last updated 2 years ago

Exakat · @Exakat
14 followers · 5 posts · Server phpc.social

: here are some irreconciliable differences between is_a() and instanceof.

For some reason, instanceof doesn't accept the :: operator : nor static method, nor class constants : only static properties.

3v4l.org/OEpgo

#php #phptip #phptrick

Last updated 2 years ago

· @dseguy
9 followers · 4 posts · Server phpc.social

allows almost all keywords as method name.

SCA hate them.

#php #phptip #phptrick

Last updated 2 years ago