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";
?>
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 ?

Cheat sheet

Comments in PHP:

  • Notes for humans - PHP ignores them
  • Can disable code temporarily without deleting it

Single-line comment (two ways):

// This is a comment
# This is also a comment
echo "Hello!"; // Comment after code

Multi-line comment:

/* This is a
   multi-line comment */

Disabling code:

// echo "This will NOT run";
echo "This will run";

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