A “Hello World” is the minimal program we use to check that a language and its environment work correctly.
You already have the server (Laragon/XAMPP) running and your editor (VS Code) ready. The moment of truth has arrived. Let’s write your first line of code in PHP.
In this article, we won’t just make text appear on the screen; we will understand how the request is processed and why PHP behaves differently from a regular HTML file.
Where do I save my files? The document root
For this environment, do not save PHP files on your Desktop or in My Documents.
For the server to process your code, the files must live inside a special folder called the Document Root.
- In Laragon: The folder is
C:\laragon\www. - In XAMPP: The folder is
C:\xampp\htdocs.
If you save the file outside of these folders, the server won’t be able to see it, and the browser won’t be able to execute it.
The basic structure: PHP tags
Inside VS Code, create a file called index.php.
For the server to know that what follows is PHP code and not simple text, we need to use the opening and closing tags.
Write this in your file:
<?php
echo "Hello World, I am Luis Llamas";
?>Let’s analyze what just happened here:
<?php: This is the switch. It tells the server: “Hey, from here on, it’s code you need to interpret/execute”.echo: This is an instruction (a language construct) used to print or display information on the output."...": Text strings always go inside quotes.;: The semicolon. In PHP, it is mandatory at the end of each instruction. If you forget it, the script will explode (literally, it will give you a Fatal Error).?>: This tells the server: “The PHP code ends here”.
Executing the script
Now, DO NOT double-click on the index.php file. If you do, the browser will show you the source code or ask you to download it.
You must access it through your local server. Open your browser (Chrome/Firefox/Edge) and type in the address bar:
http://localhost/curso-php/
If everything went well, you will see on the screen: Hello World, I am Luis Llamas.
Congratulations! Your server is alive.
What happens on the server
Here comes the technical depth part you need to understand.
In your browser, right-click on the “Hello World” text and select “View page source”. What do you see?
Hello World, I am Luis LlamasThere is no trace of <?php or echo! Why? Because PHP is executed on the Server.
The browser requested the page.
The server saw the <?php tag.
The server executed the echo command (converting code into final text).
The server sent only the result to the browser.
This is the basis of web security and dynamics. The user never sees your logic; they only see what you want them to see.
Mixing HTML and PHP
PHP was born to live embedded within HTML. Look at this example. Modify your index.php file:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to my course</h1>
<p>
<?php echo "This is written with PHP"; ?>
</p>
<p>This is normal static HTML.</p>
</body>
</html>The server will process only what is inside the PHP tags and leave the rest of the HTML intact.
Short syntax and PHP-only files
To close this module, here are two modern best practices:
The short echo tag
If you only want to print a variable or text within HTML, using <?php echo ... ?> is too verbose. There is a standard abbreviation used today:
<p><?php echo "Hello"; ?></p>
<p><?= "Hello" ?></p>The <?= is equivalent to “open PHP and immediately echo”. It’s ideal for templates.
Omitting the closing ?>
If your file contains only PHP code (no HTML), the official recommendation (PSR-12) is to NOT close the tag.
<?php
echo "Hello World";
// Do not put ?> at the endWhy? Because if you put ?> and then leave an accidental blank space or line break, that space will be sent to the browser, which can cause errors when sending HTTP headers or redirects. Save yourself the trouble: if it’s only PHP, don’t close it.