Skip to main content
PHP for Beginners
CHAPTER 03 Beginner

PHP Syntax and Basic Structure

Updated: May 12, 2026
15 min read

# Chapter 3: PHP Syntax and Basic Structure

1. Introduction

Welcome to Chapter 3! Now that your local server is running, it's time to learn the language's grammar. Just like English has rules for punctuation and sentence structure, PHP has strict rules for how code must be written. In this chapter, we will master PHP tags, understand the difference between echo and print, learn how to document our code using comments, and see how PHP seamlessly embeds into HTML.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Write properly structured PHP tags.
  • Output text and HTML using echo and print.
  • Understand the subtle differences between echo and print.
  • Add single-line and multi-line comments to your code.
  • Confidently embed PHP inside HTML documents.

3. PHP Tags

When the web server processes a .php file, it looks for special tags that tell it: "Hey, the PHP code starts here, and ends here." Anything outside of these tags is ignored by the PHP engine and sent directly to the browser as plain HTML.

The standard and universally accepted PHP tags are: <?php (Opening tag) ?> (Closing tag)

php
123
<?php
// PHP code goes here
?>

*Note: If your file contains ONLY PHP code (no HTML), it is best practice to omit the closing ?> tag. This prevents accidental whitespace from being sent to the browser, which can cause errors.*

4. Echo and Print

To display data on the screen, PHP uses two primary constructs: echo and print. They do almost the exact same thing, but there are minor differences.
  • echo: Can output multiple strings separated by commas. It has no return value and is marginally faster.
  • print: Can only output a single string and always returns the value 1.
php
123456789
<?php
// Using echo
echo "Hello with echo! <br>";
echo "This ", "string ", "was ", "combined! <br>"; // Multiple parameters

// Using print
print "Hello with print! <br>";
// print "This ", "will ", "cause ", "an error."; // Not allowed
?>

5. Comments

Comments are lines in your code that PHP ignores. They are crucial for explaining what your code does to yourself and other developers.
php
1234567891011
<?php
// This is a single-line comment

# This is also a single-line comment (shell-style)

/*
This is a multi-line comment block.
You can write paragraphs here.
It is great for temporarily disabling large chunks of code.
*/
?>

6. Embedding PHP in HTML

The true power of PHP comes from its ability to generate dynamic HTML. You can open and close PHP tags as many times as you want inside an HTML file.
php
123456789101112
<!DOCTYPE html>
<html>
<body>
    <h1>Welcome to my website</h1>
    
    <p>The current year is: <?php echo "2026"; ?></p>

    <?php
        echo "<p>You can also echo out HTML tags directly from PHP!</p>";
    ?>
</body>
</html>

7. Real-World Examples

Imagine you are building a dynamic blog layout. You want to generate article titles dynamically using PHP while keeping the structural HTML intact.
php
12345678910
<?php
// Simulating data from a database
$article_title = "Why PHP is still ruling the web";
$author = "Jane Doe";
?>

<div class="blog-card">
    <h2><?php echo $article_title; ?></h2>
    <p>Written by: <?php echo $author; ?></p>
</div>

8. Output Explanations

In the real-world example, PHP processes the variables and outputs their values inside the <h2> and <p> tags. The browser receives this HTML: <div class="blog-card"><h2>Why PHP is still ruling the web</h2><p>Written by: Jane Doe</p></div>

9. Common Mistakes

  • Forgetting Quotes: Strings in echo must be wrapped in quotes. echo Hello; will cause an error. Use echo "Hello";.
  • Misplacing Semicolons: Semicolons go inside the PHP block, not outside. <?php echo "Hi" ?>; is wrong. <?php echo "Hi"; ?> is correct.
  • PHP tags in .html files: If you save your file as index.html, the server will ignore the <?php ?> tags and the raw PHP code will leak to the user. Always use .php extensions.

10. Best Practices

  • Always use echo over print unless you specifically need the return value of print. It is the industry standard.
  • Use comments to explain *why* complex logic exists, not *what* basic code is doing. (e.g., Avoid // Echoes hello above echo "Hello";).
  • Keep indentation clean. Indent PHP code inside HTML just as you would indent nested HTML tags.

11. Exercises

  1. 1. Write a script that uses a multi-line comment to describe the goal of your website.
  1. 2. Embed a PHP block inside an HTML <ul> list, and use echo to output three <li> list items.
  1. 3. Test the difference between echo and print by trying to output multiple comma-separated strings with both.

12. Mini Project: Dynamic Webpage Intro

Task: Create an introductory webpage for a fictional company. Use PHP to inject the company name, a short description, and dynamically generate an HTML button.
php
123456789101112131415161718192021222324
<?php
// Company Data
$company_name = "Tech Solutions Inc.";
$description = "We provide cutting-edge web development services.";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo $company_name; ?> - Home</title>
</head>
<body>
    <header>
        <h1>Welcome to <?php echo $company_name; ?></h1>
        <p><?php echo $description; ?></p>
    </header>

    <main>
        <!-- Generating a button using PHP -->
        <?php echo "<button style=&#039;padding: 10px; background: blue; color: white;'>Contact Us Today</button>"; ?>
    </main>
</body>
</html>

13. Coding Challenges

Challenge 1: Create an HTML table. Use PHP inside the <td> tags to populate a row with a fake product name and a price.
php
1234567891011
<!-- Solution Snippet -->
<table border="1">
    <tr>
        <th>Product</th>
        <th>Price</th>
    </tr>
    <tr>
        <td><?php echo "Wireless Mouse"; ?></td>
        <td><?php echo "$25.99"; ?></td>
    </tr>
</table>

14. MCQs with Answers

1. Which of the following is the correct way to open a PHP block? A) <php> B) <?php C) <? D) <script php> *Answer: B*

2. What is the main difference between echo and print? A) Echo is for numbers, print is for text. B) Print can take multiple arguments, echo cannot. C) Echo can take multiple arguments and has no return value, print takes one argument and returns 1. D) There is absolutely no difference. *Answer: C*

3. How do you write a single-line comment in PHP? A) <!-- Comment --> B) // Comment C) /* Comment */ D) Comment *Answer: B*

15. Interview Questions

Q: Why is it recommended to omit the closing ?> tag in files that only contain PHP? *A:* If there is accidental whitespace or empty lines after a closing ?> tag, the server will send that whitespace to the browser as HTML output. This can break headers and cause "headers already sent" errors when working with sessions or cookies later on. Omitting the tag prevents this issue entirely.

Q: Explain how PHP and HTML interact. *A:* PHP and HTML are essentially mixed in a .php file. The server parses the document from top to bottom. When it hits a <?php tag, it switches into "PHP mode" and executes the code. When it hits ?>, it switches back into "HTML mode" and outputs everything exactly as written.

16. FAQs

Q: Can I use short tags like <? ?>? *A:* While short tags exist, they are often disabled by default on many web servers for security and compatibility reasons. Always use the full <?php ?> tags to ensure your code works everywhere.

17. Summary

You now understand the fundamental grammar of PHP. You know how to start and stop PHP parsing using the standard tags, how to display output to the browser using echo and print, and how to embed this dynamic output flawlessly inside an HTML document. You also learned the vital habit of commenting your code.

18. Next Chapter Recommendation

Outputting static text is great, but real applications handle dynamic data. In Chapter 4:
PHP Variables and Data Types**, we will learn how to store, manipulate, and update data dynamically inside the computer's memory!

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·