← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 1 of 13

PHP Introduction

PHP is a language that lives on a website's computer and builds web pages on the spot, like a cook who prepares each meal fresh when a customer orders. Visitors only see the finished page, never the recipe.

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
// PHP: Hypertext Preprocessor -- generates HTML and talks to databases
echo "Hello from the server-side!";
?>

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?

php
<!DOCTYPE html>
<html>
<body>
<h1>Mixing PHP directly into HTML</h1>
<?php // date() below runs on the server before the page is sent to the browser ?>
<p>Generated at: <?php echo date("H:i:s"); ?></p>
</body>
</html>

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
<?php
// This code runs on the server; the browser only ever sees the output below
$name = "Guest";
echo "<p>Welcome, " . $name . "</p>";
?>

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
// 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.";
?>

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
<?php
// Print "PHP_OS: " . PHP_OS . "\n" to the output
echo "PHP_OS: " . PHP_OS . "\n";
// Print "This same script runs unmodified on Windows, Linux, or macOS." to the output
echo "This same script runs unmodified on Windows, Linux, or macOS.";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Opening a .php file directly from the file system (file:///...) in a browser, which shows the raw source because PHP only runs when a server or the CLI interprets the file.
  2. Believing visitors can view your PHP code in the browser, then hard-coding secrets in the wrong place; the server runs PHP and sends only its output, but a misconfigured server can still expose source files.
  3. Saving a page with an .html extension and expecting <?php ... ?> to run, when the server only hands .php files to the PHP interpreter by default.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.