PHP Multi-line Comments
To write a comment that covers several lines, open it with /* and close it with */. PHP skips everything between those two markers.
Multi-line comment
This comment block stretches across three lines, and the echo after it runs as usual:
Prompt
Write a PHP script with a multi-line comment using /* and */ that contains "This is a multi-line comment block that spans over multiple lines", followed by echo outputting "Hello World!".
Output: PHP script
index.php
<?php
/*
This is a multi-line comment block
that spans over multiple
lines
*/
echo "Hello World!";
?>
Multi-line Comments to Ignore Code
Because these comments can wrap several lines, they are handy for switching off a whole block of code at once:
Ignore multiple lines
The two echo statements wrapped in /* and */ are skipped, and only the last one runs:
Prompt
Write a PHP script with two echo statements commented out using /* and */ (echo "Hello World!" and echo "Have a good day!"), and a third echo "Have a good evening!" that is not commented out.
Output: PHP script
index.php
<?php
/*
echo "Hello World!";
echo "Have a good day!";
*/
echo "Have a good evening!";
?>
Ignore part of a line
A multi-line comment doesn't have to fill a line. It can also disable just a piece of one, as it does here with the word "World":
Prompt
Write a PHP script that uses an inline multi-line comment /* and */ to prevent part of a line from executing. Use echo to output "Hello" followed by a comment that says "World" and then output "!".
Output: PHP script
index.php
<?php
echo "Hello" /* . " World" */ . "!";
?>