Code snippets

You will find here 258 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 ™.

This snippet shows how to compare two DateInterval objects. We can't directly use the spaceship operator on DateInterval objects o...

PHP

Published on 2022-07-13 • Modified on 2022-07-13

 View the code


This snippet shows how to extract the GET parameters of an URL with PHP. We can use parse_url(), providing the PHP_URL_QUERY as th...

PHP

Published on 2022-07-06 • Modified on 2022-07-06

 View the code


This snippet shows how to use the PHP compact function with the Symfony debug functions. Symfony dump() and dd() functions are gre...

Symfony

Published on 2022-06-30 • Modified on 2022-06-30

 View the code


In this snippet, we see how to test the presence of keys in a JSON API response. We can use the PHPUnit assertEqualsCanonicalizing...

api

Published on 2022-06-28 • Modified on 2022-06-28

 View the code


This snippet shows how to use constants in PHP 8 attributes. It is an excellent pro that PHP 8 attributes provide. Before, we had ...

PHP

Published on 2022-06-25 • Modified on 2022-06-25

 View the code


This snippet shows how to access an object property or array index with Twig dynamically. We can use the same attribute() function...

Twig

Published on 2022-06-20 • Modified on 2022-06-20

 View the code


In this snippet, we see how to calculate the number of months between two dates in PHP. Here, we don't care about the days at all....

PHP

Published on 2022-06-15 • Modified on 2022-06-15

 View the code


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