PHP: getting started
How PHP runs on the server, mixing code with HTML, and the request/response model behind almost every classic website.
A server-side language
PHP executes on the server and sends the resulting HTML to the browser. The visitor never sees your source — only its output. That is the fundamental difference from JavaScript running in the page.
<?php
// everything between these tags runs on the server
$name = "world";
echo "Hello, " . $name;💡
Never trust that PHP code is hidden: a misconfigured server can serve raw
.php as text, exposing credentials. Keep secrets outside the web root.Mixing PHP and HTML
<!DOCTYPE html>
<html>
<body>
<h1><?php echo htmlspecialchars($title); ?></h1>
<ul>
<?php foreach ($items as $item): ?>
<li><?= htmlspecialchars($item) ?></li>
<?php endforeach; ?>
</ul>
</body>
</html><?= $x ?> is shorthand for <?php echo $x; ?>. The alternative-syntax foreach (…): … endforeach; exists exactly so templates stay readable.
⚠️
Always wrap output in
htmlspecialchars() when it contains user data. Skipping it is how reflected XSS happens.Running it
php -v
php -S localhost:8000 # built-in dev server
php script.php # run a CLI script- Use the built-in server for development only; production needs a real web server.
- A file's name decides the entry point:
index.phpis the usual directory default. - Enable error display locally, and disable it in production while logging to a file.
FAQ
Is PHP still worth learning?
Yes. It powers a very large share of the web, and modern PHP (8.x) has strict types, typed properties, named arguments and a solid ecosystem.
echo or print?
Both output text;
echo accepts several arguments and is marginally faster. print returns a value, so it can be used in expressions.Related
PHP arrays Forms, sessions and safety
Last refreshed 2026-09-17.