PHP Syntax and Browser Output: Server-Side Processing Basics
Master PHP tags and the echo statement. Learn how server-side processing converts your scripts into dynamic HTML for the browser.
Previously in this course, we covered Setting Up the Local Development Environment for PHP, where we ensured your machine could execute PHP code. Now that your environment is ready, we need to understand the bridge between your code and the browser: how PHP actually runs and delivers content.
Understanding Server-Side Processing
Unlike client-side languages like JavaScript, which run inside the user's browser, PHP is a server-side language. When a user requests a page, the web server (Apache or Nginx) doesn't just send a file; it hands that file to the PHP engine first.
The PHP engine reads your file from top to bottom. It treats everything as plain text—like HTML—until it encounters a special PHP tag. When it finds a tag, it "executes" the code inside, replaces the code with the result (if any), and continues until it hits the end of the file. Finally, it sends the resulting pure HTML to the browser. The user never sees your PHP code; they only see the output.
Writing Valid PHP Tags
To tell the server "stop sending HTML and start running code," we use PHP tags. The standard and most widely accepted syntax is:
PHP<?php #6A9955">// Your code goes here ?>
You can place these tags anywhere in an HTML document. You can even mix them:
HTMLstyle="color:#808080"><style="color:#4EC9B0">h1>Welcome to my sitestyle="color:#808080"></style="color:#4EC9B0">h1> style="color:#808080"><style="color:#4EC9B0">p>Current server time: <?php echo date('H:i:s'); ?>style="color:#808080"></style="color:#4EC9B0">p>
Key rule: Always ensure your files end in the .php extension. If you save a file as .html, the web server will usually skip the PHP processing entirely, and your code will show up as plain text or be hidden by the browser.
Outputting Content with Echo
The echo command is your primary tool for sending data from the PHP engine to the browser's output buffer. It isn't a function, but a language construct, meaning it doesn't require parentheses (though you can use them if you prefer).
PHP<?php echo "Hello, World!"; echo "<h1>This is rendered as an HTML heading</h1>"; ?>
When the PHP engine processes this, it strips out the <?php ... ?> tags and replaces them with the string provided to echo. If you view the "Page Source" in your browser after running this, you will see the generated HTML, but never the echo command itself.
Hands-on Exercise: Building Our First View
In this course, we are building a small MVC web app. Let’s start by creating an entry point for our project.
- Navigate to your project folder (created in the previous lesson).
- Create a file named
index.php. - Add the following code:
PHP<!DOCTYPE html> <html> <body> <h1>My MVC Project</h1> <?php $appName = "My Awesome App"; echo "<p>Welcome to " . $appName . "</p>"; ?> </body> </html>
- Start your local server and navigate to
http://localhost:8080/index.php. You should see the rendered HTML.
Common Pitfalls
- Missing Semicolons: Every PHP statement must end with a semicolon
;. Forgetting this is the #1 cause of "Parse error: syntax error" messages. - The "White Screen of Death": If your page is completely blank, PHP might be encountering a fatal error while error reporting is turned off. Always check your server logs or ensure
display_errorsis set toOnin yourphp.iniduring development. - Opening/Closing Tag Issues: While the closing
?>tag is optional at the very end of a file, it is best practice to omit it if the file contains only PHP code. This prevents accidental "whitespace" (empty lines) from being sent to the browser, which can cause issues with HTTP headers later on.
FAQ
Q: Can I use print instead of echo?
A: Yes, print works similarly to echo, but echo is slightly faster and is the standard convention in the PHP community.
Q: Does PHP output to the browser console?
A: No. PHP runs on the server. If you want to debug data in the browser console, you must use echo to output it into a <script> tag or use a specialized logging tool.
Q: Why does my PHP code show up in the browser source?
A: This happens if your web server isn't configured to parse .php files. Re-visit the previous lesson to ensure your server is correctly linked to the PHP executable.
Recap
We’ve learned that PHP is a server-side language that generates HTML on the fly. We use <?php ... ?> tags to demarcate code and echo to output data. By understanding that the browser only ever sees the result of our PHP script, we can effectively manage the transition from raw logic to rendered front-end content.
Up next: Variables and Data Types — we'll move beyond static strings and learn how to store and manipulate dynamic data in our application.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.


