PHP Hello World: Your First Script and Essential Tags Guide

🚀 What you will learn today:

Now that we have configured our PHP development environment, it's time to see your code come to life!

  • How to create a hybrid document (HTML + PHP).
  • Where to save your files in XAMPP (Windows, Mac, and Linux).
  • The different PHP tag variations and which one to use.
  • The semicolon golden rule.

📚 Master PHP once and for all!

Get all this content organized with exclusive exercises and offline access. The ultimate guide for your coding career.

DOWNLOAD COMPLETE GUIDE (PDF) →

Hello, World! in PHP

Open your favorite text editor (VS Code, Notepad++, or even Sublime Text). A script is essentially a list of instructions that the PHP interpreter will read and execute line by line, from top to bottom.

Let's create a hybrid document, mixing HTML's visual structure with PHP's dynamic intelligence. Type the following code:

<html>
 <head>
  <title>PHP Progressive Course</title>
 </head>
 <body>
   Hello world! I am in HTML! <br/> 
   <?php
     echo "Hello, World! I am in PHP!";
   ?>
 </body>
</html>

Where to save your PHP files (XAMPP)

Unlike a regular HTML file, you cannot simply save PHP anywhere and double-click it. For the server to process the code, the file must have the .php extension and be located in the server's public folder.

📁 Path by Operating System:

  • Windows (XAMPP): Save to C:\xampp\htdocs\home.php
  • Mac: Save to /Applications/XAMPP/htdocs/home.php
  • Linux: Save to /opt/lampp/htdocs/home.php (or /var/www/html).

After saving, open your browser and type: localhost/home.php.

PHP Hello World result in browser

Understanding the <?php ?> tag

What happened behind the scenes? The server detected the .php extension and triggered the interpreter. The secret lies in the <?php ... ?> tag. Everything inside it is treated as programming logic.

The echo command sends text directly to the document. This is why we call PHP an embedded language: it lives inside the HTML but is processed on the server before reaching the user.

⚠️ Golden Rule: In PHP, every instruction must end with a semicolon (;). Forgetting this is the #1 cause of "Parse Error" for beginners!

PHP Tag Variations

While the standard tag is recommended, it is important to know other variations you might find in legacy code or specific server configurations.

1. Short Tags

Uses only <? ... ?>. It is more compact but might be disabled in the php.ini file (short_open_tag directive). We do not recommend using it if you plan to distribute your code across different servers.

<?
  echo "Hello World with short tags!";
?>

2. Script Tags (Deprecated)

In the past, a syntax similar to JavaScript was used. Note that this is deprecated in modern PHP versions:

<script language="php">
  echo "Hello via script tag";
</script>

3. Inline Echo (Short Echo Tag)

Useful for quick outputs within HTML tags:

<?php echo "Direct text"; ?>
✅ Verdict: Always prefer <?php ?>. It is universal and ensures your code works on any server (Linux, Windows, or Mac) without extra tweaks.

Deepen your studies:
How Apache Server works
Introduction to MySQL Databases


Nenhum comentário:

Veja também: