Create Your First PHP File
Every PHP file uses the extension .php. Code wrapped in
<?php ... ?> tags is executed by PHP - everything outside the tags
is sent to the browser as-is.
Create a file called hello.php with this content:
<?php
echo "Hello, World!";
That is a complete, working PHP program. Three things to notice:
<?php- opening tag that tells the engine "PHP starts here"echo- language construct that outputs a value;- semicolon ends every PHP statement
When a file contains only PHP, omit the closing ?>. This avoids accidental whitespace being sent to the browser, which would trigger "headers already sent" errors later.
Run via the Browser (Apache)
If you installed XAMPP or Laragon, save hello.php in your web root:
- XAMPP:
C:\xampp\htdocs\hello.php - Laragon:
C:\laragon\www\hello.php - MAMP:
/Applications/MAMP/htdocs/hello.php
Start Apache and open http://localhost/hello.php in your browser. You should see:
Run via PHP Built-in Server
PHP ships with a small development web server. Perfect for quick experiments - no Apache
needed. Open a terminal in the folder containing hello.php and run:
php -S localhost:8000
Now visit http://localhost:8000/hello.php. Press Ctrl+C to stop the server.
The PHP built-in server handles one request at a time and is not for production use. Use Nginx + PHP-FPM or Apache + mod_php in production.
Run from the Command Line
You can run a PHP file directly from the terminal - no web server, no browser:
php hello.php
# or explicitly
php -f hello.php
# One-liner without a file
php -r 'echo "Hello from CLI!";'
This is how cron jobs, deploy scripts, and Composer commands run PHP.
echo vs print
PHP has two output constructs. They are almost identical but differ slightly:
<?php
// echo - accepts multiple arguments, no return value
echo "Hello", " ", "World", "!\n";
// print - one argument only, returns 1 (usable in expressions)
print "Hello World\n";
$ok = print "Stored"; // $ok = 1
// printf - C-style formatted output
printf("Name: %s, Age: %d\n", "Ruban", 30);
| Feature | echo | |
|---|---|---|
| Returns value | No | Yes (always 1) |
| Multiple arguments | Yes (comma-separated) | No |
| Speed | Marginally faster | Marginally slower |
| Common use | Default choice | Inside expressions only |
Mixing PHP with HTML
The real power of PHP is mixing dynamic data into static HTML:
<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body>
<h1>Welcome, <?php echo "Ruban"; ?>!</h1>
<p>The server time is: <?php echo date("H:i:s"); ?></p>
<ul>
<?php for ($i = 1; $i <= 3; $i++): ?>
<li>Item <?php echo $i; ?></li>
<?php endfor; ?>
</ul>
</body>
</html>
Notice the alternative syntax for: ... endfor; which is friendlier inside HTML than curly braces.
Short Echo <?= ?>
A common shortcut for echoing a single expression is <?= ... ?>:
<h1>Hello <?= htmlspecialchars($name) ?>!</h1>
<p>Year: <?= date("Y") ?></p>
<!-- equivalent to -->
<h1>Hello <?php echo htmlspecialchars($name); ?>!</h1>
<p>Year: <?php echo date("Y"); ?></p>
Any value coming from a user, database, or external source must be escaped with htmlspecialchars() before echoing into HTML. Otherwise you have an XSS vulnerability.
Common First-Run Errors
You opened the file directly (file:///C:/...). PHP must be processed by a server. Either start Apache and use http://localhost/ or run php -S localhost:8000 in the folder.
You forgot a semicolon, closing brace, or closing quote. The line number in the error message is where PHP noticed the problem, not necessarily where it started.
A fatal error occurred but display_errors is off. Add this to the top of your script while debugging:
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);Next Steps
You wrote and ran your first PHP program. Now learn the language fundamentals: