Code snippets

You will find here 126 code snippets I find useful so I don't have to Google ™ or Stackoverflow ™ them when I need. There are also code snippets I always forget! 😁 Sometimes, related links to the code will be available. The main difference with other random snippets you will find on the web is that in most cases (for the Symfony related ones) you will be able to run them directly from this website to check the output so you can verify it works as expected ™.

In this snippet, we will see how to get a variable from an include. Here, we affect the return value of an include call to the $de...

PHP

Published on 2020-07-25 • Modified on 2020-07-25

 View the code


In this snippet, we will see how to extract a result from an array using the array_reduce PHP function and a closure. I get all th...

PHP

Published on 2020-07-11 • Modified on 2020-07-11

 View the code


I always forget this one. We must set the preferred_state setting. Once the package is installed, you can restore the previous set...

PHP

Published on 2020-04-01 • Modified on 2020-04-01

 View the code


In this snippet, we will see how to sort a PHP array by descending numerical value. That the type of code I sometimes forget and w...

PHP

Published on 2020-02-07 • Modified on 2020-02-07

 View the code


The str_split function is not well known. It converts a string into an array of characters (with the default second parameter). It...

PHP

Published on 2019-12-05 • Modified on 2019-12-05

 View the code


In this snippet, we will see how to read and modify a private property of a PHP object. I recently had a concrete case when I have...

PHP

Published on 2019-12-03 • Modified on 2020-02-03

 View the code


[Edit 2022-04-27] I use PHP 8 attributes now, click on the "read the doc" link to see a relevant example. 🙃 This is a small trick...

Doctrine

Published on 2019-11-07 • Modified on 2022-04-27

 View the code


This is how to modify all values of a PHP array with an anonymous function (closure). Note that when using array_map, the closure ...

PHP

Published on 2019-11-03 • Modified on 2019-11-03

 View the code


Sometimes you need to access an application's constant as a parameter in the container service file. You can directly use the PHP ...

Symfony

Published on 2019-10-25 • Modified on 2019-10-25

 View the code


This is a simple trick to get a random value of an array created on the fly. Of course, in this case, the array must have unique v...

PHP

Published on 2019-09-09 • Modified on 2019-09-09

 View the code