Comments are used to explain code, make it easier to understand, and to temporarily disable code without deleting it.
Click to reveal answer
beginner
How do you write a single-line comment in PHP?
You can write a single-line comment using either // or # at the start of the line.
Click to reveal answer
beginner
How do you write a multi-line comment in PHP?
Multi-line comments start with /* and end with */. Everything between is ignored by PHP.
Click to reveal answer
beginner
Can comments affect the execution of PHP code?
No, comments are ignored by the PHP engine and do not affect how the code runs.
Click to reveal answer
beginner
Example: What will this PHP code output?
<?php
// echo 'Hello';
echo 'World';
?>
The output will be World because the first line is a comment and does not run.
Click to reveal answer
Which of the following is NOT a valid way to write a comment in PHP?
A<!-- This is a comment -->
B# This is a comment
C// This is a comment
D/* This is a comment */
✗ Incorrect
Option A is an HTML comment, not a PHP comment.
What symbol starts a multi-line comment in PHP?
A//
B<!--
C#
D/*
✗ Incorrect
Multi-line comments in PHP start with /* and end with */.
What happens to the code inside comments when PHP runs?
AIt runs normally
BIt is ignored
CIt causes an error
DIt runs twice
✗ Incorrect
Code inside comments is ignored by PHP and does not run.
Which comment style can be used for a single line in PHP?
A<!-- and -->
B/* and */
C// and #
DNone of the above
✗ Incorrect
Single-line comments can use // or # in PHP.
Why might a programmer use comments in their PHP code?
ATo explain the code to others
BTo speed up the code
CTo make the code run slower
DTo delete code permanently
✗ Incorrect
Comments help explain code to others or yourself later.
Explain the different ways to write comments in PHP and when you might use each.
Think about quick notes versus longer explanations.
You got /4 concepts.
Describe how comments affect PHP code execution and why they are important.
Consider what happens when PHP runs code with comments.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a correct way to write a single-line comment in PHP?
easy
A.
B. // This is a single-line comment
C. /* This is a single-line comment */
D. # This is not a comment
Solution
Step 1: Identify single-line comment syntax in PHP
PHP supports single-line comments using // and #.
Step 2: Check each option
// This is a single-line comment uses // correctly. is HTML comment syntax, not PHP. /* This is a single-line comment */ is multi-line comment syntax. # This is a single-line comment is also valid in PHP, so # This is not a comment is correct as well.
Final Answer:
// This is a single-line comment -> Option B
Quick Check:
Single-line comment = // or # [OK]
Hint: Single-line comments start with // or # in PHP [OK]
Common Mistakes:
Confusing HTML comments with PHP comments
Using multi-line comment syntax for single-line comments
Thinking # is not a valid comment
2. Which of the following is the correct syntax for a multi-line comment in PHP?
easy
A. /* This is a multi-line comment */
B. // This is a multi-line comment
C. # This is a multi-line comment
D.
Solution
Step 1: Recall multi-line comment syntax in PHP
PHP uses /* ... */ to write multi-line comments.
Step 2: Evaluate each option
/* This is a multi-line comment */ correctly uses /* ... */. Options B and C are single-line comment styles. is HTML comment syntax.
Final Answer:
/* This is a multi-line comment */ -> Option A
Quick Check:
Multi-line comment = /* ... */ [OK]
Hint: Multi-line comments use /* at start and */ at end [OK]
Common Mistakes:
Using // or # for multi-line comments
Confusing HTML comments with PHP comments
Forgetting to close multi-line comment with */
3. What will be the output of the following PHP code?
<?php
// echo 'Hello';
echo 'World';
?>
medium
A. HelloWorld
B. No output
C. Hello
D. World
Solution
Step 1: Understand the effect of the comment
The line // echo 'Hello'; is commented out, so it does not run.
Step 2: Identify the executed code
The line echo 'World'; runs and prints "World".
Final Answer:
World -> Option D
Quick Check:
Commented out code does not run, so output = World [OK]
Hint: Code after // is ignored, only uncommented code runs [OK]
Common Mistakes:
Thinking commented code runs
Confusing output by combining commented and uncommented lines
Ignoring that echo prints text
4. Find the error in the following PHP code:
<?php
/* This is a comment
echo 'Hello';
// Missing closing tag
?>
medium
A. PHP tags are incorrect
B. Using // inside multi-line comment is invalid
C. Missing closing */ for multi-line comment
D. echo statement syntax error
Solution
Step 1: Check multi-line comment syntax
The multi-line comment starts with /* but never ends with */.
Step 2: Understand consequences of missing closing comment
Without closing */, PHP treats the rest as comment, causing syntax errors or unexpected behavior.
Final Answer:
Missing closing */ for multi-line comment -> Option C
Quick Check:
Multi-line comments must close with */ [OK]
Hint: Always close multi-line comments with */ [OK]
Common Mistakes:
Forgetting to close multi-line comments
Thinking // cannot be inside multi-line comments
Misreading PHP tags as error
5. You want to temporarily disable a block of code without deleting it. Which comment style should you use in PHP to do this safely for multiple lines?
hard
A. Use /* at start and */ at end of the block
B. Use # at the start of the block
C. // Comment each line with //
D. Wrap the block with
Solution
Step 1: Understand commenting multiple lines
Using /* ... */ allows commenting multiple lines at once safely.
Step 2: Evaluate other options
// Comment each line with // requires adding // on each line, which is tedious. Use # at the start of the block is invalid for multiple lines. Wrap the block with is HTML comment syntax, not PHP.
Final Answer:
Use /* at start and */ at end of the block -> Option A
Quick Check:
Multi-line comments use /* ... */ [OK]
Hint: Use /* ... */ to comment multiple lines quickly [OK]
Common Mistakes:
Using # or // for many lines without repeating
Confusing HTML comments with PHP comments
Not closing multi-line comments properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.