Beginner php PHP 15 min read

Template Text vs. PHP Code

Template Text vs. PHP Code

Getting Your PHP Web Server Running

When you're starting out with PHP, it's important to understand how to view your code in a web browser. If your PHP file isn't named index.php, you'll need to specify its name when accessing it. For instance, if you're working with a file called hello.php, you’ll need to navigate to localhost:8000/hello.php in your browser after starting the PHP web server with the command php -S localhost:8000.

Sometimes, you might encounter an error indicating that the port you're trying to use (typically 8000) is already in use. If this happens, simply try an alternative port like 8001, 8080, or 8888. It's also a good idea to investigate which application is already using that port – you might have another web server running in the background. While running multiple web servers isn't inherently a problem, it’s generally best to only test one at a time to ensure you're seeing the responses from the application you intend to work on. If you consistently have port conflicts, you can track down the process using your operating system's tools and stop it, or reboot your computer.

Blending Structure and Logic: Template Text and PHP Code

PHP’s popularity in web development stems from its ability to seamlessly integrate HTML (and other web technologies like CSS and JavaScript) for display in a browser. A typical PHP page isn't just raw HTML; it's a carefully constructed combination of two key components: static template text and dynamically generated content.

Consider an online store's shopping cart. A developer doesn't need to create a separate HTML file for every possible cart configuration. Instead, a single PHP script combines a reusable template – containing elements like the HTML head, navigation bar, and company logo – with PHP code that dynamically populates the cart's details: item names, prices, quantities, and more. This mixture allows for a structured foundation while providing personalized, data-driven content.

This powerful combination is why we're using the <?php and ?> tags in our scripts. Any text outside these tags is treated as plain template text and is displayed exactly as it appears in the file. Conversely, anything inside these tags is interpreted as PHP code and executed. This distinction allows for the creation of dynamic web pages that are both visually consistent and responsive to user interactions and data.

Understanding Template Text and PHP Code

When working with PHP, it's crucial to differentiate between plain text (often called "template text") and actual PHP code. They are interleaved within a PHP file to produce the final output. The way PHP interprets these sections depends on how they're marked.

When a line contains only PHP code – for instance, a print statement – only the opening <?php tag is required. However, if PHP code is followed by template text, you must include the closing ?> tag. Let's illustrate this with an example.

A Simple Demonstration

Create a new PHP file, which we’re calling hello2.php, either in your preferred online environment (like Replit) or using a local IDE. Paste the following content into this file:

I am template text, not PHP code.
print "Hello, world!\n";
I am more template text.

Now, execute this script from your command line using the command php hello2.php. You’ll observe the following output:

I am template text, not PHP code.
print "Hello, world!\n";
I am more template text.

Notice how the entire content of the file is printed verbatim, including the PHP code. This happens because the script lacks proper delimiters. PHP doesn't know where the PHP code begins and ends, so it treats everything as template text. The unexpected inclusion of the print keyword, quotation marks, and semicolon in the output highlights this issue.

The terminal prompt appearing immediately after the last line is another consequence of this. Extra whitespace and newline characters are treated as part of the template text and output directly, causing the terminal to begin a new prompt without adding an extra line break. If the file ended with a blank line, the terminal prompt would be on its own line.

Correcting the Script

To fix this, we need to clearly delineate the PHP code block. Here's the corrected hello2.php file, with the changes highlighted:

I am template text, not PHP code.
<?php
print "Hello, world!\n";
?>
I am more template text.

By adding the <?php and ?> tags, we explicitly tell PHP to interpret the line containing print "Hello, world!\n"; as PHP code. Now, when you run the script, PHP will execute the print statement and output "Hello, world!" to the console, while treating the other lines as plain text. This distinction is fundamental to properly structuring PHP applications.