Code snippets

You will find here 211 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 see how to test a role of an arbitrary user with Symfony. We know to do it for the current user, thanks to the...

Symfony

Published on 2022-06-12 • Modified on 2022-06-12

 View the code


This snippet shows how to fetch a Doctrine entity from a repository with the correct type hint. We can use the find() function to ...

Doctrine

Published on 2022-05-14 • Modified on 2022-05-14

 View the code


This snippet shows how to declare a string in PHP with a Heredoc and Nowdoc block. This one is for me as I always forget how to do...

PHP

Published on 2022-04-30 • Modified on 2022-04-30

 View the code


This snippet shows how to get the type name of a variable for debugging. The get_debug_type() function was introduced in PHP 8 and...

PHP

Published on 2022-04-23 • Modified on 2022-04-23

 View the code


This snippet shows how to check if a string contains another string with PHP. This is a kind of tribute to our "old" PHP. The str...

PHP

Published on 2022-04-15 • Modified on 2022-04-15

 View the code


This snippet shows how to translate the day and month of a date with PHP. We use the intl extension and its IntlDateFormatter clas...

PHP

Published on 2022-04-12 • Modified on 2022-04-12

 View the code


In this snippet, we see how to remove useless decimal of number_format with PHP. This trick is only helpful if you want to have lo...

PHP

Published on 2022-04-04 • Modified on 2022-04-04

 View the code


This snippet shows how to get the last part of an URL with PHP. It's a kind of a relic of the past! Indeed, this basename() functi...

PHP

Published on 2022-03-25 • Modified on 2022-03-25

 View the code


In this snippet, we see how to filter a Doctrine collection. We have this filter() function we can call with a closure. Here I use...

Doctrine

Published on 2022-03-19 • Modified on 2022-03-19

 View the code


This snippet shows how to get the IRI of an entity with API Platform (and the opposite!). I have added this snippet because I noti...

API Platform

Published on 2022-03-14 • Modified on 2022-03-14

 View the code