PHP Introduction
What is PHP?
PHP stands for 'PHP: Hypertext Preprocessor' (a recursive acronym), an open-source scripting language purpose-built for the web. Unlike a general-purpose language bolted onto a server, PHP was designed from the start to generate HTML and talk to databases, which is why it's still the engine behind a huge share of the web's backend code.
Example: What is PHP?
<?php
// PHP: Hypertext Preprocessor -- generates HTML and talks to databases
echo "Hello from the server-side!";
?>
Login to try C/C++/Java/PHP code in the editor
Why Learn PHP?
PHP is approachable because you can mix it directly into HTML and see results immediately, without a compile step or heavy project scaffolding. It's also genuinely fast for typical web workloads and has built-in functions for the things web apps do constantly: sanitizing input, hashing passwords, and talking to databases.
Example: Why Learn PHP?
<!DOCTYPE html>
<html>
<body>
<h1>Mixing PHP directly into HTML</h1>
<p>Generated at: <?php echo date("H:i:s"); ?></p>
</body>
</html>
Login to try C/C++/Java/PHP code in the editor
How PHP Works
PHP is a server-side language, meaning the .php file never reaches the visitor's browser. The web server runs the PHP code, PHP produces plain HTML (or JSON, or whatever text output you choose), and only that output is sent to the browser — the visitor can't view your PHP source through 'View Page Source'.
Example: How PHP Works
<?php
// This code runs on the server; the browser only ever sees the output below
$name = "Guest";
echo "<p>Welcome, " . $name . "</p>";
?>
Login to try C/C++/Java/PHP code in the editor
Applications of PHP
Because PHP runs before the page reaches the browser, it can do things a browser-only language can't: read and write files on the server, query a database, check session state, and assemble a page's content dynamically based on who's asking for it and what they submitted.
Example: Applications of PHP
<?php
// PHP can read/write files, unlike a browser-only language
file_put_contents('visits.txt', "Visitor logged\n", FILE_APPEND);
echo "Visit recorded on the server.";
?>
Login to try C/C++/Java/PHP code in the editor
Benefits of PHP
PHP runs on Windows, Linux, and macOS, and every major web host and cloud provider supports it out of the box, which is a big part of why it remains so widely deployed — you rarely have to fight your hosting environment to get PHP working.
Example: Benefits of PHP
<?php
echo "PHP_OS: " . PHP_OS . "\n";
echo "This same script runs unmodified on Windows, Linux, or macOS.";
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: