How to Comment Out PHP

Learn how to comment out PHP code and find out the most common mistakes to watch out for.

Published Categorized as PHP

Every so often, as you write PHP code for your website or web application, you may want to disable a few lines of code without necessarily deleting them. To do this, you can comment those lines out. This tutorial will show you how to do it.

Suppose you have the following two functions in your PHP code:

<?php
	function funcOne($param) {
		echo $param;
	}
	funcOne("Hello world!");

	function funcTwo($param) {
		echo $param;
	}
	funcTwo("Hello world!");
?>

For one reason or another, you want to comment funcTwo out.

Multi-Line Comments

To comment out multiple lines of PHP code, add a forward slash followed by an asterisk (/*) to the beginning of the code block and an asterisk followed by a forward slash (*/) to the end.

This turns your code into a comment, and the PHP interpreter parses it without executing it.

Here’s what the commented-out code block from our example looks like:

<?php
	function funcOne($param) {
		echo $param;
	}
	funcOne("Hello world!");

	/* 
	function funcTwo($param) {
		echo $param;
	}
	funcTwo("Hello world!");
	*/
?>

You can also add notes to your comments to make your source code easier for others to read:

<?php
	function funcOne($param) {
		echo $param;
	}
	funcOne("Hello world!");

	/* Deprecated - 2/2023
	function funcTwo($param) {
		echo $param;
	}
	funcTwo("Hello world!");
	*/
?>

Adding descriptive comments to your PHP code is especially important if you’re part of a team or you work in a large corporation, and your code is part of a program that’s built collaboratively with other people who may not know why and when you commented out a particular chunk of code.

Single-Line Comments

In PHP, you can also create single-line comments by adding two forward slashes (//) to the start of the line.

This is useful when you want to temporarily disable a single line of code, such as the calling of a PHP function. For example, you can turn this:

funcTwo("Hello world!");

Into this, which would once again prevent the interpreter from executing the code:

// funcTwo("Hello world!");

Things to Know

  • Choose the right method. Generally, single-line comments are best for commenting single lines out, and multi-line comments for commenting multiple lines out. If you’re still not sure which one you should use, check your team’s (or your company’s) coding style guide.
  • Close your comments correctly. Make sure to close the comments in the right place, or everything else in your code will be treated as a comment as well, which will case your website’s or application’s logic to break.

By Dim Nikov

Editor of Maker's Aid. Part programmer, part marketer. Making things on the web and helping others do the same since the 2000s. Yes, I had Friendster and Myspace.

Leave a comment

Your email address will not be published. Required fields are marked *