PHP Comments and Output Statements Practice Questions with solutions

Introduction

PHP comments help programmers explain their code, while output statements display information on a webpage. In PHP, echo and print are commonly used to display text, numbers, HTML, and variable values. Learning these concepts will help you write readable PHP programs and understand how websites display dynamic content. PHP Comments and Output Statements Practice Questions with solutions to help you understand the concepts.

Question 1: Display a Message Using echo

Problem Statement: Write a PHP program that displays a welcome message using the echo statement.

PHP Solution


<?php
echo "Welcome to PHP Programming!";
?>

Sample Output

Welcome to PHP Programming!

Explanation

  • echo displays output.
  • Text is enclosed in quotation marks.
  • The semicolon ends the statement.

The echo statement is one of the most frequently used ways to display output in PHP.

Question 2: Display Multiple Values Using echo

Problem Statement: Display a student’s name, age, and course using separate echo statements.

PHP Solution


<?php
echo "Name: Rahul<br>";
echo "Age: 18<br>";
echo "Course: PHP";
?>

Sample Output

Name: Rahul

Age: 18

Course: PHP

Explanation

Each echo statement displays one piece of information. The <br> HTML tag creates a line break in the browser.

You can use this approach to display student details, employee information, or other simple records.

Question 3: Display Output Using the print Statement

Problem Statement: Write a PHP program that displays a message using print.

PHP Solution


<?php
print "PHP is easy to learn!";
?>

Sample Output

PHP is easy to learn!

Explanation

The print statement displays a string or another printable value.

Both echo and print can display output. A key difference is that print returns the integer value 1, while echo does not return a value.

Question 4: Use a Single-Line Comment

Problem Statement: Write a PHP program that uses a single-line comment to explain the output statement.

PHP Solution


<?php
// Display a welcome message
echo "Welcome to PHP Practice!";
?>

Sample Output

Welcome to PHP Practice!

Explanation

The // symbols start a single-line comment. PHP ignores the comment when executing the program.

Comments help you and other programmers understand the purpose of the code.

Question 5: Use a Multiline Comment

Problem Statement: Write a PHP program using a multiline comment before displaying a student’s information.

PHP Solution


<?php
/*
This program displays
basic student information.
*/

echo "Student Name: Priya<br>";
echo "Course: PHP Programming";
?>

Sample Output

Student Name: Priya

Course: PHP Programming

Explanation

A multiline comment begins with /* and ends with */. It can contain several lines of notes.

PHP does not execute the text inside the comment.

Question 6: Use the # Comment Symbol

Problem Statement: Display two messages using PHP, with each statement explained by a # comment.

PHP Solution


<?php
# Display the first message
echo "Learn PHP basics.<br>";

# Display the second message
echo "Practice PHP daily.";
?>

Sample Output

Learn PHP basics.

Practice PHP daily.

Explanation

PHP supports # as another way to write a single-line comment. It works similarly to //.

Both styles are valid, although // is more commonly used in many PHP codebases.

Question 7: Display HTML Using echo

Problem Statement: Use PHP to display a heading and a paragraph on a webpage.

PHP Solution


<?php
echo "<h1>My PHP Website</h1>";
echo "<p>I am learning web development.</p>";
?>

Sample Output

My PHP Website

I am learning web development.

Explanation

PHP can generate HTML elements through echo. The browser interprets the generated HTML and displays the heading and paragraph.

This technique is useful when building dynamic webpages.

Question 8: Display Variables Using echo and print

Problem Statement: Display an employee’s name and salary using both echo and print.

PHP Solution


<?php
$name = "Amit";
$salary = 25000;

echo "Employee Name: " . $name . "<br>";
print "Monthly Salary: " . $salary;
?>

Sample Output

Employee Name: Amit

Monthly Salary: 25000

Explanation

  • $name stores the employee’s name.
  • $salary stores the salary amount.
  • The dot (.) joins strings and variable values.
  • echo and print display the combined result.

This is a simple example of using output statements with variables.

Question 9: Display Output Using the Short Echo Tag

Problem Statement: Display a student’s name and course using the PHP short echo tag.

PHP Solution


<?php
$name = "Neha";
$course = "PHP";
?>

<h2>Student Details</h2>
<p>Name: <?= $name ?></p>
<p>Course: <?= $course ?></p>

Sample Output

Student Details

Name: Neha

Course: PHP

Explanation

The <?= ... ?> syntax is shorthand for <?php echo ...; ?>. It is particularly convenient when inserting variable values into HTML.

For example, <?= $name ?> displays the value stored in $name.

For untrusted user input, escape output appropriately with htmlspecialchars() to prevent HTML injection and cross-site scripting.

Question 10: Combine Comments, echo, and print

Problem Statement: Create a student report that uses comments, echo, and print to display student information.

PHP Solution


<?php
// Store student details
$name = "Rohan";
$course = "PHP Programming";
$marks = 85;

/*
Display the student report
using output statements.
*/

echo "<h2>Student Report</h2>";
echo "Name: " . $name . "<br>";
print "Course: " . $course . "<br>";
echo "Marks: " . $marks;
?>

Sample Output

Student Report

Name: Rohan

Course: PHP Programming

Marks: 85

Explanation

This program combines several concepts:

  • Single-line comments explain the variables.
  • A multiline comment describes the report.
  • echo displays the heading, name, and marks.
  • print displays the course.
  • The dot (.) concatenates text and variable values.

These techniques are useful for displaying reports and other dynamic information on PHP websites.

Key Takeaways

  • echo and print display output in PHP.
  • echo can output multiple comma-separated expressions; print accepts one expression.
  • print returns 1, whereas echo has no return value.
  • // and # create single-line comments.
  • /* ... */ creates multiline comments.
  • Comments are ignored during normal PHP execution.
  • PHP can generate HTML using output statements.
  • The short echo tag <?= ... ?> is useful inside HTML templates.
  • Escape untrusted values before displaying them in HTML.

Frequently Asked Questions

1. What are comments in PHP?

Comments are notes in source code that PHP does not execute. They help programmers explain the purpose of code.

2. What is the difference between echo and print?

Both display output. echo does not return a value and can accept multiple comma-separated expressions, while print accepts one expression and returns 1.

3. Which is faster, echo or print?

For everyday PHP development, the performance difference is generally insignificant. Choose the statement that makes your code easiest to understand.

4. Can I use HTML inside echo?

Yes. For example, echo "<h1>Hello</h1>"; generates an HTML heading that the browser displays.

5. What is the purpose of // in PHP?

It starts a single-line comment. PHP ignores the comment text until the end of the line.

6. Can comments contain multiple lines?

Yes. Use /* to begin a multiline comment and */ to end it.

7. What does <?= mean in PHP?

It is the short echo tag, used to output an expression within an HTML template.

8. Can comments be used to temporarily disable code?

Yes. You can comment out code while testing or debugging, but avoid commenting out large sections unnecessarily.

9. Is it safe to display user input directly with echo?

Not always. Escape untrusted text with htmlspecialchars() when displaying it in HTML to help prevent cross-site scripting attacks.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top