Codectionary / Developer documentation / PHP

Start here to learn what PHP is, how it powers websites, where it runs, what developers build with it, and how to run a first program.

File extension
.php
Runs in
PHP runtime on a server or CLI

A beginner’s path

Start with this overview to understand where PHP runs and why a website might need it. Then write a short program using a local PHP installation. Continue with variables and strings, arrays, conditions, functions, request data and database access. You do not need a framework or a database for your first program.

Codectionary’s Playground can edit and highlight PHP files, but it cannot execute PHP. To see PHP output, run it with a local PHP command-line installation or a web server configured for PHP. A browser opening a .php file directly does not run its PHP code. If PHP is not installed yet, choose the Windows, macOS or Linux installation guide below.

What PHP is and why it exists

PHP is an open-source, general-purpose scripting language especially suited to web development. Its name today is the recursive acronym “PHP: Hypertext Preprocessor.” A PHP script is a program: it can calculate values, make decisions, call functions, read data and produce output. It can also be embedded among HTML markup, which made it approachable for people building web pages.

PHP is often called a server-side language because a web server can pass a request to a PHP runtime before sending a response to the visitor. The browser receives the resulting HTML, JSON or other response, not the PHP source that produced it. PHP also runs from a terminal without a browser or web server, which makes it useful for scripts and scheduled tasks.

The original need was practical: make pages respond to data and user actions instead of serving the same static file to everyone. A page can now show a signed-in user’s account, search a catalogue or save a submitted form. “Dynamic” describes that changing response; it does not mean every PHP page needs a database or a large framework.

From personal web tools to PHP

Rasmus Lerdorf created the earliest PHP tools in 1994 as CGI programs written in C. He first used them to track visits to his online résumé. They were called “Personal Home Page Tools,” and he released their source code publicly in June 1995 so others could use and improve them. Form processing and database interaction became part of the growing toolkit.

PHP/FI (“Forms Interpreter”) grew beyond the original collection of utilities. In the late 1990s, Andi Gutmans and Zeev Suraski worked with Lerdorf on a major rewrite; PHP 3 adopted the name “PHP: Hypertext Preprocessor.” Later releases improved the engine, object-oriented features, error handling and language syntax. PHP 7 brought major performance improvements, and PHP 8 added features such as union types, named arguments and the match expression.

Modern PHP is developed in the open by contributors, with release managers coordinating supported branches and proposals discussed through the PHP RFC process. The official manual and supported-versions page are better guides to current behaviour than tutorials written for older PHP releases. Existing websites and content-management systems, a large library ecosystem and broad hosting support help explain why PHP remains a practical choice for web development.

How a PHP website responds to a request

Imagine that you request /courses.php. The browser sends an HTTP request to the website. The web server routes the PHP page to a configured PHP runtime, often through a server module or FastCGI. PHP executes the script, which may read request values, ask a database for course records and assemble a response. The web server returns that response; the browser then displays the HTML or processes the JSON it receives. The exact server configuration varies by host.

The script can choose a different response for different requests. For example, a GET request might display a course list, while a POST request might submit a form. PHP can set a status code and headers as well as a response body. A server can also cache a response; “dynamic” does not require recomputing every page on every visit.

A PHP file is not a self-contained website server. PHP needs its runtime, and browser-facing pages need a server configured to hand requests to it. The PHP command-line interface is a separate way to run scripts; it needs no browser. The built-in PHP server is useful for local development, but the PHP manual does not recommend it as a public production server.

StepWhat happensWhat the visitor receives
RequestThe browser asks the web server for a URL.Nothing yet.
ExecutionThe server asks PHP to run the matching script; PHP may use a database or other service.The PHP source stays on the server.
ResponsePHP produces a body and, where needed, headers and a status code.HTML, JSON or another response format.
DisplayThe browser interprets the response and runs any browser-side JavaScript.The rendered page or data requested by the app.

PHP, HTML, CSS and JavaScript work together

HTML describes the page structure: headings, forms, links and other elements. CSS controls presentation, including spacing, colour and responsive layout. JavaScript commonly runs in the browser to respond to clicks or update a page without a full navigation. PHP usually runs on the server first and can generate the HTML or data that those browser technologies use.

For a course search, HTML provides the search form, CSS styles it, and JavaScript might offer suggestions as the person types. PHP can validate the submitted search term, query stored courses and return a results page or JSON for the JavaScript interface. PHP may send JavaScript or CSS as text, but it does not execute browser DOM code on the server.

A form illustrates the boundary. The browser sends named fields in a request; PHP can read values through request data such as $_GET or $_POST. Those values are untrusted. Check their expected type and format before using them, use parameterised queries for database values, and escape text for its output context before placing it into HTML.

Why web applications often use databases

Variables in a single request are not a permanent store for user accounts, orders or course progress. A database can keep records between requests and let the application retrieve or update them. PHP commonly uses database extensions such as PDO to connect to a supported database. The database is a separate service or file; installing PHP alone does not create one.

Consider an online shop: PHP receives a request for a product, asks the database for its name and stock level, and renders a page. During checkout, the application must validate input, manage authentication, protect payment-related data and handle failures. The language makes these workflows possible, but a secure application depends on sound design, configuration and testing.

Where PHP is useful

The same language can serve a small page or support a larger application. The tools around it differ: a content-management system provides publishing features, a framework can organise routes and dependencies, and a database stores records. PHP itself does not automatically supply all of these parts.

ApplicationA practical exampleWhy PHP fits
Dynamic websitesShow today’s available university workshops.Generate a response from current data instead of maintaining a separate static page for every change.
Backend servicesProcess a booking submitted by a browser or mobile app.Run validation and business rules on a server that clients do not control.
Web applicationsTrack a learner’s saved lessons and progress.Combine requests, sessions and persistent records across visits.
Content managementPublish and edit articles in WordPress.PHP powers established CMS software and its extensions.
E-commerceDisplay a product catalogue and record an order.Connect page generation with inventory and order data; payments need separate secure integration.
REST APIsReturn a JSON list of lessons to an app.Read a request and send structured data with an appropriate HTTP status.
AuthenticationCheck credentials and maintain a signed-in session.Use password hashing, sessions and server-side access checks together.
Database-driven appsSearch a catalogue of books.Use a database extension such as PDO to query stored records.
Command-line toolsImport course records from a local file.Run PHP scripts directly from a terminal without a browser.
AutomationGenerate a scheduled report.Reuse PHP code in tasks run by a scheduler or deployment workflow.

Advantages and limitations

PHP is approachable for a first server-side program: a small script can produce a page, and common hosting environments already support it. Its standard library, database extensions, Composer packages and established frameworks give teams ways to grow beyond a single file. PHP supports procedural, object-oriented and other programming styles, so a project can be structured to fit its size.

There are tradeoffs. PHP does not run directly in an ordinary browser, so learners need a suitable runtime to see results. Server configuration, extensions and PHP versions can differ between development and hosting. Dynamic code must validate input and handle authentication, database access and output escaping carefully; choosing PHP does not provide security automatically. Legacy PHP projects may use practices or syntax that should not be copied into new code without checking current documentation.

PHP is often a good choice when an existing PHP application or ecosystem fits the task. A static page may need only HTML and CSS; browser-only interactivity may need JavaScript; a performance-sensitive or specialised service may have different constraints. Choose based on the system you need to build and maintain, not a claim that one language solves every problem.

Write and run a first PHP program

The first example below creates two variables and prints two lines. Save it as hello.php and run php hello.php in a terminal where PHP is installed. The expected output is “Hello from PHP!” followed by “You have 3 lessons to explore.” The <?php opening tag tells PHP where code starts; the $ prefix marks variables; echo writes output; semicolons end these statements. Double-quoted strings replace $language and $lessons with their values, while \n starts a new output line.

The second example is a tiny web page with a form. Save it as index.php and serve its folder using a PHP-capable local web server. On the first visit, the form appears without a greeting. After submitting “Sam,” PHP reads the POST value, trims it and returns HTML containing “Hello, Sam!” The is_string check avoids treating a submitted array as text; htmlspecialchars encodes special characters before putting the name into HTML. This is an introductory example of response generation, not a complete authentication or data-storage system.

Change the first example’s language name and predict its output before running it again. Then continue to variables, conditions, arrays and functions. If the terminal says php is not recognised, PHP may not be installed or available on your PATH. If a browser shows raw PHP source, the file was opened directly or the server is not configured to execute PHP.

Terminal — after saving hello.php

php hello.php

Syntax

<?php
$language = "PHP";
echo "Hello from $language!\n";

Examples

Print two lines from a PHP script

Save as hello.php and run with the local PHP CLI. The output is two lines: Hello from PHP! and You have 3 lessons to explore.

<?php
$language = "PHP";
$lessons = 3;
echo "Hello from $language!\n";
echo "You have $lessons lessons to explore.\n";
  1. The opening tag <?php begins PHP code. The $language and $lessons variables hold a string and a number.
  2. echo sends text to standard output in a terminal. Inside double quotes, PHP inserts the variable values; \n adds a newline after each message.
  3. Running php hello.php executes the saved file. Editing the variable values changes the two output lines without changing the echo statements.

Respond to a submitted name with HTML

Save as index.php and serve it through a PHP-enabled local server. Submit a name to see a greeting added to the returned page.

<?php
$rawName = $_POST['name'] ?? '';
$name = is_string($rawName) ? trim($rawName) : '';
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>PHP greeting</title>
</head>
<body>
  <form method="post">
    <label for="name">Your name</label>
    <input id="name" name="name" required>
    <button type="submit">Greet me</button>
  </form>
  <?php if ($name !== ''): ?>
    <p>Hello, <?php echo htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>!</p>
  <?php endif; ?>
</body>
</html>
  1. The form sends its named field as a POST request to the same URL. $_POST retrieves submitted values; ?? supplies an empty string on the first visit.
  2. The value might not be a string, so is_string checks its type before trim removes surrounding spaces. The conditional emits a greeting only when a non-empty name remains.
  3. htmlspecialchars converts characters such as < and & for HTML text. For a submitted name of Sam, the page shows Hello, Sam!; the PHP source itself stays on the server.

PHP: background & uses

Used for
Dynamic websites, Content management, Server APIs
Created by
Rasmus Lerdorf
First released
1995 · first public release
Version / standard
PHP 8.5

Current stable release line. Hosting providers may offer several supported versions.

In the real world

  • WordPress: The WordPress content-management system
  • Wikipedia: The MediaWiki platform behind the site
  • Drupal: The Drupal content-management system

Facts checked 8 September 2026. Product links describe documented uses.

Best practices

  • Run PHP examples with a local PHP runtime or a PHP-enabled web server; Codectionary’s Playground currently supports editing and highlighting PHP, not executing it.
  • Treat form values as untrusted input. Check their type and validity, use parameterised database queries, and escape output for the context where it appears.
  • Use the official PHP manual and supported-versions page when a project depends on a particular feature or release.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading