Hi, I'm Liam

I talk about code and stuff

Quickly Comment Out a PHP File

Published on

What do you do if you need to “comment out” a whole PHP file?

You could prefix every line with //, wrap the whole thing in a /* */ block, wrap it in if (false) { }, but none of those are very elegant. In most IDEs, if you select the whole file CMD+A and use a hotkey to comment it CMD+/, it’ll actually wrap it in HTML comments <!-- --> because the code outside the PHP tags is typically HTML.

There’s a little technique I spotted Nuno Maduro using in his talk at Laracon US 2024 - it feels obvious in hindsight - just add an early return immediately after the opening PHP tag - then nothing after it will be loaded at all:

<?php return;

none_of_this_code_will_execute();
Photo of Liam Hammett
written by
Liam Hammett
Found a typo? Suggest a fix here!
Hit me up on Twitter / GitHub / LinkedIn / email me
Copyright © 2024 Liam Hammett and all that kind of stuff