Menu
Coddy logo textTech

Comments

Part of the Fundamentals section of Coddy's PHP journey — lesson 4 of 71.

Comments are notes you write inside your code. PHP completely ignores them - they exist only to help humans understand the code.

To write a single-line comment, use //. Everything after // until the end of the line is ignored:

<?php
// This is a comment
echo "Hello World!";
?>

PHP also supports a second way to write single-line comments, using the # symbol. It works exactly the same as //:

<?php
# This is also a comment
echo "PHP is great!";
?>

A comment can also be written at the end of a line, after the code:

<?php
echo "Hello World!"; // This prints Hello World!
?>

For comments that span several lines, use /* to start and */ to end:

<?php
/* This is a multi-line comment.
   PHP ignores all of it. */
echo "Welcome!";
?>

Comments can also temporarily disable a line of code without deleting it:

<?php
// echo "This line will NOT run";
echo "This line will run";
?>

Note: Every multi-line comment that starts with /* must be closed with */, otherwise PHP treats everything after it as a comment.

challenge icon

Challenge

Beginner

Fix the code so that only Hello, PHP! is printed.

  • Replace the ? with the symbols that turn a line into a comment
  • The line printing Goodbye! should become a comment so it does NOT run
  • Only change the line that starts with ?

Try it yourself

<?php
// Type your code below
? echo "Goodbye!";
echo "Hello, PHP!";
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals