PHP Hello World: Your First Script and Essential Tags Guide

🚀 In this Lesson:

Now that your local development environment is ready, let's bring your code to life.

  • Building your first hybrid document (HTML + PHP).
  • Mastering the htdocs directory structure.
  • Understanding the Golden Semicolon Rule.
  • Exploring PHP tag variations and modern standards.

Hello, World! — Your First Dynamic Script

In programming, a script is simply a text file containing a sequence of instructions. Open your editor (VS Code is recommended) and let's create a file that mixes static HTML with dynamic PHP logic. Type the following code exactly as shown:

<!DOCTYPE html>
<html>
 <head>
  <title>Progressive PHP Mastery</title>
 </head>
 <body>
    <h1>Static Content: Hello from HTML!</h1>
    
    <?php 
        // This is where the magic happens
        echo "<p>Dynamic Content: Hello from PHP!</p>"; 
    ?>
 </body>
</html>

The Critical Step: Where to Save Your Files

Unlike a standard .html file, you cannot simply double-click a .php file from your desktop. For the PHP interpreter to "see" your code, it must be placed inside the web server's root directory.

📁 XAMPP Default Directories:

  • Windows: C:\xampp\htdocs\
  • macOS: /Applications/XAMPP/htdocs/
  • Linux: /opt/lampp/htdocs/

Save your file as hello.php inside the htdocs folder.

How to run it:

  1. Ensure Apache is running in your XAMPP Control Panel.
  2. Open your browser and navigate to http://localhost/hello.php.
PHP Result in Browser

Behind the Scenes: How PHP is Processed

When you request hello.php, the server doesn't just send the text to your browser. Instead:

  1. The server looks for the <?php tag.
  2. Everything inside that tag is sent to the PHP Interpreter.
  3. The interpreter executes the commands (like echo) and generates pure HTML.
  4. The server sends only the resulting HTML back to the user.

Note: If you right-click the page in your browser and select "View Page Source", you will notice that the PHP tags are gone. The user never sees your raw backend logic.

Syntax Fundamentals & The Golden Rule

PHP is flexible, but it is also strict about its grammar. Mastering these two rules now will save you hours of debugging later:

⚠️ The Semicolon (;) is Mandatory

Every PHP statement must end with a semicolon. Forgetting a single ; will trigger a Parse Error and crash your script. It is the #1 mistake for beginners.

Tag Variations: Which one to use?

In the wild, you might encounter different ways to open PHP code. Here is the modern verdict:

  • Standard Tags (<?php ... ?>): The universal standard. Use this 100% of the time for maximum compatibility across all servers.
  • Short Echo (<?= $var ?>): A clean way to output a single variable inside HTML. Very common in modern templates.
  • Short Open Tags (<? ... ?>): Avoid these. They depend on server configuration (short_open_tag in php.ini) and can cause your code to fail on different hosting providers.

Pro-Tip: Separation of Concerns

While mixing HTML and PHP in the same file is easy for beginners, professional developers eventually move toward "Separation of Concerns." As you progress in this course, you will learn how to keep your logic (PHP) and your presentation (HTML) in separate files to create maintainable, clean codebases.

Next Step: Variables and Data Types

Writing text is great, but real programming involves storing and manipulating data. Let's move to the next lesson!

Continue to Next Lesson →

Nenhum comentário:

Veja também: