Code snippets

You will find here 108 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 test a getter and a setter simultaneously in a PHPUnit test case. This small trick allows us to gain spa...

PHP

Published on 2022-10-26 • Modified on 2022-10-26

 View the code


This snippet shows how to check if a Symfony route exists. We mustn't use the getRouteCollection() method because it regenerates t...

Symfony

Published on 2022-10-22 • Modified on 2022-10-22

 View the code


This snippet shows how to use the Symfony string component to get the singular or plural of a word. It isn't easy to do this as th...

Symfony

Published on 2022-10-15 • Modified on 2022-10-15

 View the code


This snippet shows how to generate a slug with the Symfony string component. Usually, when using the Symfony string component, we ...

Symfony

Published on 2022-10-11 • Modified on 2022-10-11

 View the code


This snippet shows how to use the new first-class callable syntax introduced in PHP 8.1. Creating a callback has never been the mo...

PHP

Published on 2022-10-02 • Modified on 2022-10-02

 View the code


This snippet shows how to get the calling function name in a debug message with PHP. That's a common need when you want to log or ...

PHP

Published on 2022-09-22 • Modified on 2022-09-22

 View the code


This snippet shows how to get the FQCN of a variable with ::class; this is new functionality introduced in PHP 8.1. Before, we cou...

PHP

Published on 2022-09-11 • Modified on 2022-09-11

 View the code


This snippet shows how to test if an array is a list with PHP. This function was introduced in PHP 8.1, before we had to do this m...

PHP

Published on 2022-09-10 • Modified on 2022-09-10

 View the code


This snippet shows how to use the PHP null coalescing assignment operator. One learns every day! I had totally forgotten about th...

PHP

Published on 2022-09-09 • Modified on 2022-09-09

 View the code


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