Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 147 ยท Getting Started

Getting Started with PHP

Meet PHP, the language behind a huge share of the web. Learn what it is, where it runs, what it powers, and write and run your first PHP 8.5 scripts right in your browser.

PHP is a programming language built for the web. When you open a page on a site powered by PHP, a program on the server runs, gathers data (from a database, a file, the URL you typed), builds the page, and sends the finished HTML to your browser. You never see the PHP code itself, only what it produced.

PHP has been around since 1995, and it has changed enormously. Modern PHP 8 is fast, has a real type system, classes, enums, attributes and much more. It runs WordPress, Wikipedia, Laravel and Symfony applications, online shops, forums, APIs and countless internal business tools. Learning it gives you a direct path to building real websites.

This lesson explains what PHP is and where it runs, then gets you writing code straight away. Every example on this page has a live editor: press Run (or Ctrl + Enter) and the code runs on our server with PHP 8.5, and the output appears under the editor.

What PHP is

PHP is a server-side scripting language. Each part of that phrase means something:

  • Server-side: the code runs on a computer that serves web pages (the server), not in the visitor's browser. JavaScript in a page runs in the browser; PHP runs before the page is even sent.
  • Scripting language: you write plain text files ending in .php, and PHP runs them directly. There's no separate compile step for you to do. (Behind the scenes PHP does compile your code into an internal format, as you'll see in How PHP Works.)

The name is a recursive acronym: PHP: Hypertext Preprocessor. "Hypertext" refers to HTML, and "preprocessor" describes the original idea: PHP processes a file before it reaches the browser, filling in the dynamic parts.

PHP is also general-purpose. Besides web pages, it can run from the command line to process files, send emails in the background, import data, or power long-running workers. Our editors use exactly that mode, the command line, which is why output appears as plain text.

Your first script

Here is a complete PHP program. Run it:

PHP Hello, PHP

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

A few things to notice:

  • <?php opens PHP mode. Everything after it is PHP code. A PHP file can also contain plain HTML outside these tags, which you'll explore in PHP Syntax.
  • echo sends text to the output. On a web server, that output becomes the page the browser receives. Here, it appears below the editor.
  • Text values are called strings and go inside quotes. 'single' and "double" quotes both work; double quotes understand special sequences such as "\n", which means "new line".
  • Each statement ends with a semicolon ;.

Try deleting one of the semicolons and running again. PHP refuses to run the script and prints a Parse error that names the line where it got confused. Error messages are your friends: reading them carefully is half of programming.

Doing calculations

PHP understands arithmetic, so it works as a calculator too. echo can print several values separated by commas, and PHP_EOL is a built-in constant holding the end-of-line character:

PHP PHP as a calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The output is:

TEXT
12
12
42
3.1428571428571
3
1
1024

Anything after // on a line is a comment: a note for humans that PHP ignores. You'll learn all the comment styles in Comments.

Variables

A variable stores a value under a name so you can use it later. In PHP, every variable name starts with a dollar sign $. You create a variable simply by assigning to it with =:

PHP Variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Inside double quotes, PHP replaces $temperature and $city with their values. This is called interpolation. Single quotes print the text exactly as written, dollar sign and all. That difference is one of the first things every PHP developer learns; Strings covers it in depth, and Variables explains naming rules and more.

Tip: Use single quotes for plain text and double quotes when you want variables or escape sequences like \n inside. Many style guides follow this rule, and it makes your intent obvious.

PHP builds HTML

On a real website, PHP's output is usually HTML. The script below builds a small list from data. Our editor shows output as plain text, so you'll see the HTML source; on a web server, the browser would render it as a bulleted list:

PHP Generating HTML

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

This is the core trick of PHP: data plus code produces a page. Change the array and the page changes, without editing any HTML by hand. A few new pieces appear here:

  • ['PHP', 'JavaScript', 'SQL'] is an array, a list of values. Arrays get a whole chapter, starting with Arrays.
  • foreach repeats a block of code once for every item. See foreach Loops.
  • The dot . joins strings together (concatenation).
  • htmlspecialchars() makes text safe to put inside HTML, so a value like <script> can't turn into real markup. Escaping output is a security habit you'll use in every web page; Preventing XSS explains why it matters.

Reading input

Command-line programs can read what a user types. In our editor, the Input box under the code plays the role of the keyboard: each line is one line of typed input. fgets(STDIN) reads the next line, and trim() removes the newline at its end:

PHP Asking a question

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

(int) converts the typed text into a whole number so you can calculate with it, and date('Y') returns the current year as a string. Change the lines in the Input box and run again. On a website, input arrives differently, through forms and URLs, which the Handling Forms lesson covers.

Where PHP runs

You can run PHP in several places, and you'll meet all of them:

Where How it works Typical use
Web server A server such as nginx or Apache hands each request to PHP (usually PHP-FPM), and PHP's output becomes the HTTP response. Websites, APIs
Command line (CLI) You run php script.php in a terminal. Output goes to the terminal. Scripts, cron jobs, tools, queue workers
Built-in dev server php -S localhost:8000 serves a folder for local testing. Development only
This tutorial's editor Your code is sent to our server and run with the PHP 8.5 CLI in a sandbox. Learning and experimenting

The language is the same everywhere. Differences are in the surroundings: a web request has URL parameters, cookies and headers; a CLI script has command-line arguments and a terminal. How PHP Works walks through the request cycle in detail.

About this editor: each run starts fresh on a sandboxed server with no network access and no web server, and output is plain text. Lessons about forms, cookies and sessions show how they work on a real server and then simulate requests in runnable code, so you can still experiment.

What PHP powers

PHP is one of the most widely deployed server languages. A few examples of what people build with it:

  • Content management systems: WordPress, Drupal and Joomla are written in PHP. WordPress alone runs a large share of all websites.
  • Frameworks for custom applications: Laravel and Symfony provide routing, databases, queues and security tools for building anything from a startup's product to a bank's internal systems. You'll get an overview in PHP Frameworks and CMSs.
  • E-commerce: shop systems such as WooCommerce, Magento (Adobe Commerce) and Shopware.
  • APIs and back ends for mobile apps and JavaScript front ends.
  • Command-line tools: Composer, PHP's package manager, is itself a PHP program.

PHP's popularity comes from practical strengths: cheap hosting almost everywhere, a gentle start (one file can be a whole page), a huge standard library, and a "shared-nothing" model where each request starts clean, which makes scaling simple.

PHP versions and the release cycle

PHP gets a new feature release every year, usually in late November. Each version receives two years of active support (bug fixes) followed by two years of security fixes only, then it reaches end of life. Running an end-of-life version means no more security patches, so keep your servers current.

A quick history of the versions you'll hear about:

Version Released Why it matters
5.x 2004โ€“2014 Classes became solid; much old code online is from this era.
7.0 2015 About twice as fast as 5.6; scalar type declarations.
8.0 2020 JIT, named arguments, match, union types, the nullsafe operator.
8.1 2021 Enums, readonly properties, fibers.
8.2โ€“8.3 2022โ€“2023 Readonly classes, typed class constants, json_validate().
8.4 2024 Property hooks, asymmetric visibility, array_find().
8.5 2025 The pipe operator |>, clone with changes, array_first().

This tutorial teaches modern PHP 8.5 and labels features added after 8.0 with their version, so you know what works on older servers. When you search the web for help, check the date: advice from the PHP 5 era (such as the removed mysql_* functions) is often outdated or unsafe. What's New in PHP 8.0 to 8.5 collects the highlights.

You can always check which version is running:

PHP Which PHP is this?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP_SAPI prints cli, short for command-line interface. On a web server running PHP-FPM it would print fpm-fcgi.

How this tutorial is organized

The course moves from the basics to building real applications:

  1. Getting Started: syntax, comments, output, variables and constants.
  2. Data types and operators: strings, numbers, booleans, type conversion and every operator.
  3. Control flow and functions: decisions, loops, and reusable code.
  4. Strings and arrays in depth: PHP's two workhorse data structures.
  5. Object-oriented PHP: classes, interfaces, traits, enums and modern features.
  6. Errors, files and the web: exceptions, files, JSON, forms, cookies and sessions.
  7. Databases and security: PDO, prepared statements, password hashing and safe coding.
  8. Advanced topics and projects: generators, attributes, testing, deployment, and complete projects.

Each lesson ends with a short quiz. Take them: recalling an idea right after you learn it is one of the most effective ways to make it stick.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where does PHP code run when someone visits a PHP website?

    PHP is server-side. The server runs the script and sends only its output (usually HTML) to the browser.
  2. 2. What does this print? $n = 5; echo 'n is $n';

    Single quotes don't interpolate variables, so the text is printed exactly as written. With double quotes it would print "n is 5".
  3. 3. Which character must every PHP variable name start with?

    PHP variables always start with a dollar sign, such as $city.
  4. 4. What usually happens to a PHP script's output on a web server?

    On a web server, everything the script outputs (with echo, HTML outside PHP tags and so on) becomes the body of the response.
  5. 5. How long does a PHP version receive security fixes in total, from its release?

    Two years of active support followed by two years of security-only support. After that the version is end of life.

Summary

  • PHP is a server-side scripting language: it runs on the server and sends its output, usually HTML, to the browser.
  • Scripts start with <?php; statements end with ;; echo prints output.
  • Variables start with $. Double-quoted strings interpolate variables; single-quoted strings don't.
  • PHP runs behind web servers, on the command line, and in this tutorial's editor (the CLI, with plain-text output).
  • PHP powers WordPress, Laravel and Symfony applications, shops and APIs.
  • A new PHP version arrives every year with four years of support; this course teaches PHP 8.5 and labels newer features.

Next: How PHP Works, where you'll follow a request from the browser to PHP and back, and see how to install PHP on your own computer.