PHP

The server-side language that runs most of the web.

What is it?

PHP is a server-side programming language built specifically for the web. While JavaScript runs in the visitor's browser, PHP runs on the web server: when someone requests a page, the server executes your PHP code - reading a database, checking a login, processing a form - and sends back the resulting HTML.

It powers a huge share of the web (WordPress alone runs on PHP), and its killer feature for beginners is deployment: PHP comes pre-installed on virtually every shared hosting plan. Upload a .php file and it just runs - no server processes to manage, no configuration.


When to use it

PHP is the most practical choice when you're on shared hosting or building a traditional website - pages rendered on the server, a contact form, a small database-backed site. It pairs naturally with MySQL, which the same hosts also provide. If you're building a JavaScript-heavy single-page app or want one language across frontend and backend, Node.js may fit better.


Core concepts

  • Embedded in HTML - PHP code lives inside <?php ... ?> tags in otherwise normal HTML files. The server runs the code and the visitor only ever sees the output.
  • The request/response cycle - each page load is a fresh run of your script. Form data arrives in $_POST and $_GET; sessions ($_SESSION) carry data between requests.
  • Variables and arrays - variables start with $; PHP's array type doubles as both list and dictionary and is used everywhere.
  • Talking to databases - the built-in PDO library connects to MySQL and others. Always use prepared statements, which keep user input from being executed as SQL.
  • Escaping output - run user-provided text through htmlspecialchars() before printing it, so it can't inject scripts into your page.

A taste of the code

<?php
$name = $_GET["name"] ?? "world";
$hour = (int) date("H");
$greeting = $hour < 12 ? "Good morning" : "Hello";
?>
<!doctype html>
<html>
  <body>
    <h1><?= htmlspecialchars("$greeting, $name!") ?></h1>
  </body>
</html>

Save this as hello.php on any PHP host, visit hello.php?name=Sam, and the server builds the page for that visitor on the fly.


Your first steps

  1. Install PHP locally (on Windows, XAMPP is the easy route; on Mac/Linux, PHP is a package install away), then run php -S localhost:8000 in your project folder for an instant dev server.
  2. Make a page that uses date() and an if statement to greet visitors differently by time of day.
  3. Build a contact form: an HTML form that posts to a PHP script which validates the input.
  4. Connect to a MySQL database with PDO and build a tiny guestbook - insert and list messages. Learn some SQL alongside.
  5. Upload your project to a shared host and see it run on the real web.

Common beginner mistakes

  • "Headers already sent" errors. Any output - even a blank line or invisible byte-order mark before <?php - stops you setting cookies or redirecting. In files that contain only PHP, leave off the closing ?> tag.
  • Loose comparisons. == converts types in surprising ways. Use === unless you specifically want conversion.
  • Following tutorials that use mysql_* functions. They were removed in PHP 7. Use PDO with prepared statements.
  • Echoing user input unescaped. Wrap it in htmlspecialchars() every time.
  • Ignoring Composer. It's PHP's package manager and autoloader; modern libraries and frameworks assume you use it.

Getting it online

PHP runs on virtually every shared hosting plan: upload your files to public_html and they work. In the control panel, select the newest PHP version available, enable the free SSL certificate, and make sure errors are logged rather than displayed. If your plan includes SSH access, you can run Composer on the server too.

Our PHP + MySQL guide builds and uploads a complete, secure example.


← Back to Stack Picker All primers Compare languages