PHP Syntax, Tags and Basic Structure Practice Questions with Solutions

Introduction

PHP syntax refers to the rules used to write PHP code correctly. Before learning variables, loops, functions, and database programming, you should understand PHP tags, statements, semicolons, comments, case sensitivity, and how PHP code works inside HTML.

In this chapter, you will practice solved PHP questions with working code, sample output, and simple explanations. These examples are suitable for beginners learning PHP from zero. PHP Syntax, Tags and Basic Structure Practice Questions with Solutions to help you understand the concepts.

Question 1: Write PHP Code Using Opening and Closing Tags

Problem Statement: Write a PHP program using PHP opening and closing tags to display a welcome message.

PHP Solution


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

Sample Output

Welcome to PHP Programming!

Explanation

  • <?php is the opening PHP tag.
  • echo displays the message.
  • ; ends the statement.
  • ?> is the closing PHP tag.

PHP code is written between PHP tags when it is embedded in an HTML document. In a file containing only PHP code, the closing tag is usually omitted.

Question 2: Display Two Messages Using Separate Statements

Problem Statement: Write a PHP program that displays two messages on separate lines.

PHP Solution


<?php
echo "I am learning PHP.<br>";
echo "I practice PHP every day.";
?>

Sample Output

I am learning PHP.

I practice PHP every day.

Explanation

Each echo statement displays a message. The semicolon ends each statement, while the HTML <br> tag moves the next message to a new line in the browser.

Question 3: Understand the Importance of Semicolons

Problem Statement: Write a PHP program containing three statements, each ending with a semicolon.

PHP Solution


<?php
echo "My name is Rahul.";
echo " I am learning PHP.";
echo " PHP is useful for web development.";
?>

Sample Output

My name is Rahul. I am learning PHP. PHP is useful for web development.

Explanation

The semicolon ; marks the end of a PHP statement. Multiple statements can appear on separate lines or on the same line.

If a required semicolon is missing, PHP may report a syntax error. In this example, each statement is correctly terminated.

Question 4: Write PHP Code Inside an HTML Document

Problem Statement: Create an HTML webpage that uses PHP to display a heading and a paragraph.

PHP Solution


<!DOCTYPE html>
<html>
<head>
    <title>PHP Syntax Practice</title>
</head>
<body>

    <h1>PHP Learning</h1>

    <?php
    echo "<p>PHP works with HTML.</p>";
    ?>

</body>
</html>

Sample Output

PHP Learning

PHP works with HTML.

Explanation

HTML defines the webpage structure, while PHP generates the paragraph. The web server executes the PHP code and sends the resulting HTML to the browser.

Save this program as question4.php and run it through your local PHP server.

Question 5: Write Single-Line Comments in PHP

Problem Statement: Write a PHP program using single-line comments to explain the code.

PHP Solution


<?php
// Display the student's name
echo "Student Name: Rahul";

// Display the student's course
echo "<br>Course: PHP";
?>

Sample Output

Student Name: Rahul

Course: PHP

Explanation

PHP supports // and # for single-line comments. Comments help explain code and are ignored by the PHP interpreter.

For example:

// This is a comment
# This is also a comment

Question 6: Use Multiline Comments in PHP

Problem Statement: Create a PHP program that uses a multiline comment before displaying a message.

PHP Solution


<?php
/*
This program displays
a message for PHP beginners.
*/

echo "Start learning PHP today!";
?>

Sample Output

Start learning PHP today!

Explanation

Multiline comments begin with /* and end with */. They can contain multiple lines of notes and are not executed as PHP statements.

Use comments to explain important logic without adding unnecessary descriptions to every line.

Question 7: Understand Case Sensitivity in PHP

Problem Statement: Display three messages using different capitalizations of the echo keyword.

PHP Solution


<?php
echo "First message<br>";
ECHO "Second message<br>";
EcHo "Third message";
?>

Sample Output

First message

Second message

Third message

Explanation

PHP keywords such as echo, if, and else are generally case-insensitive. Therefore, echo, ECHO, and EcHo work.

However, PHP variable names are case-sensitive. For example, $name and $Name are different variables. Using lowercase keywords consistently is recommended for readable code.

Question 8: Display Text Using Single and Double Quotes

Problem Statement: Write a PHP program that displays text using both single quotes and double quotes.

PHP Solution


<?php
echo 'Welcome to PHP';
echo "<br>";
echo "Learn PHP step by step";
?>

Sample Output

Welcome to PHP

Learn PHP step by step

Explanation

PHP supports both single-quoted and double-quoted strings.

  • Single quotes are useful for displaying literal text.
  • Double quotes can interpret variables and certain escape sequences.

For example, when $name contains Rahul, echo "Hello $name"; displays Hello Rahul, while echo 'Hello $name'; displays the literal text Hello $name.

Question 9: Use the Short Echo Tag

Problem Statement: Display a welcome message using PHP’s short echo tag.

PHP Solution


<!DOCTYPE html>
<html>
<body>

    <?= "Welcome to PHP Practice!" ?>

</body>
</html>

Sample Output

Welcome to PHP Practice!

Explanation

The <?= ... ?> syntax is a shorthand for <?php echo ...; ?>. It is useful when displaying values inside HTML templates.

For example:

<?php echo "Hello"; ?>

is equivalent to:

<?= "Hello" ?>

The short echo tag is available without needing to enable the traditional short-open-tag setting.

Question 10: Combine PHP Tags, Comments, HTML and Multiple Statements

Problem Statement: Create a student information webpage using PHP tags, comments, multiple statements, and HTML.

PHP Solution


<!DOCTYPE html>
<html>
<head>
    <title>Student Information</title>
</head>
<body>

    <h1>Student Information</h1>

    <?php
    // Display student details
    echo "<p>Name: Rahul</p>";
    echo "<p>Course: PHP Programming</p>";

    /*
    Display the student's
    current learning level.
    */
    echo "<p>Level: Beginner</p>";
    ?>

</body>
</html>

Sample Output

Student Information

Name: Rahul

Course: PHP Programming

Level: Beginner

Explanation

This example combines the main concepts covered in this chapter:

  • PHP opening and closing tags.
  • HTML structure and PHP-generated content.
  • Single-line and multiline comments.
  • Multiple statements ending with semicolons.
  • Text displayed through echo.

These basics make it easier to understand more advanced PHP programs.

Key Takeaways

  • PHP code typically starts with <?php.
  • The closing tag ?> is optional at the end of a PHP-only file.
  • PHP statements generally end with semicolons.
  • Use // or # for single-line comments and /* ... */ for multiline comments.
  • PHP can be embedded in HTML documents.
  • PHP keywords are generally case-insensitive, but variable names are case-sensitive.
  • Single-quoted and double-quoted strings behave differently when variables are included.
  • The short echo tag <?= ... ?> is useful for displaying values in HTML templates.

Frequently Asked Questions

1. What is PHP syntax?

PHP syntax is the set of rules for writing valid PHP code, including tags, statements, expressions, and comments.

2. Why are PHP tags required?

PHP tags identify the sections of a file that the PHP interpreter should execute.

3. Is a semicolon required in PHP?

Most PHP statements require a semicolon. Omitting one can cause a syntax error, although some constructs have different rules.

4. Can PHP code be written inside HTML?

Yes. PHP can be embedded in HTML, allowing a server to generate dynamic webpage content.

5. Are PHP keywords case-sensitive?

No. PHP language keywords such as echo and if are generally case-insensitive. Variable names are case-sensitive.

6. What is the difference between // and /* */?

// starts a single-line comment, while /* */ surrounds a comment that can span multiple lines.

7. What is the difference between <?php echo and <?=?

Both can display output. The <?= form is a shorthand for the echo statement.

8. What happens if PHP tags are missing?

If PHP code is outside PHP tags in a mixed HTML/PHP file, the server generally treats it as ordinary text instead of executing it. PHP-only files should begin with the opening PHP tag.

9. Should I include the closing PHP tag?

In a file containing only PHP code, it is generally recommended to omit the closing tag to avoid accidental whitespace output.

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

Scroll to Top