Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PHP
PHP
0/12 completed
Beginner
Hello PHPVariables and arraysControl flowFunctions
Intermediate
Strings, dates and JSONForms, requests and sessionsDatabases with PDO
Advanced
Object-oriented PHPErrors, exceptions and typesSecurity essentials
Pro
Composer, autoloading and testingLaravel basics
Tracks / PHP / Beginner

Hello PHP

PHP runs a huge share of the web. WordPress alone powers over 40% of all websites, and Wikipedia, Facebook's early years, Slack's backend, Etsy and countless shops and dashboards were built with it. PHP was designed for the web: a PHP program usually runs on a server for each request and produces the HTML (or JSON) the browser receives. Modern PHP (version 8 and later) is fast, has types, classes, enums and a great ecosystem with Composer and Laravel.

💡 The editors in this track run real PHP 8.5 (the official PHP, compiled to WebAssembly) in your browser, like the php command line. The first run downloads about 7 MB; after that it's instant. Change the examples and run them again.

Your first program

PHP · editable
  • PHP code lives between <?php and (optionally) ?>. Anything outside those tags is sent out as-is, which is how PHP mixes code into HTML.
  • echo prints text. "\n" is a newline.
  • Every statement ends with a semicolon.
PHP · editable
Statement Use
echo Print one or more values
printf / sprintf Print / build formatted text (%s, %d, %.2f)
var_dump Show a value with its type (the go-to debugging tool)
print_r Show arrays readably

Variables always start with $. They don't need to be declared: assigning creates them.

PHP and HTML

PHP's original superpower: drop code into an HTML page. Here's a page that a server would send to a browser:

PHP · editable
  • <?= … ?> is short for <?php echo … ?>.
  • foreach (…): … endforeach; is an alternative syntax that's easier to read inside HTML.
  • htmlspecialchars() makes text safe to put in HTML. You'll see why in Security essentials.

Modern apps usually keep HTML in templates (Blade in Laravel, Twig in Symfony) rather than mixing it with logic, but the idea is the same.

Comments

PHP · editable

Errors

PHP tells you the problem, the file and the line:

PHP · editable

A warning (the undefined variable) prints a message and carries on; an error like DivisionByZeroError stops the script unless you catch it. Always read the first message: later ones are often side effects.

Running PHP on your computer

  1. Install PHP 8.3 or newer:
    • macOS: brew install php
    • Ubuntu/Debian: sudo apt install php-cli (or the ondrej/php PPA for the newest version)
    • Windows: download from windows.php.net, or use WSL; or install Laravel Herd (macOS/Windows), which sets up PHP, Composer and a web server in one click
  2. Check: php -v
  3. Run a script: php hello.php
  4. Try code interactively: php -a
  5. Serve a folder as a website, with PHP's built-in server: php -S localhost:8000, then open http://localhost:8000

Practice

  1. Print your name, city and age in one sentence with variables inside double quotes.
  2. Use printf to print a price with exactly two decimal places.
  3. Use var_dump on a number, a string, true and null and compare the output.
  4. Build a small HTML table of three products with foreach.
✎ Check your understanding

1.Which line correctly prints "Hi Asha" when $name = "Asha"?

2.What does <?= $title ?> do?

3.Which function shows a value together with its type?

4.What happens after a warning such as "Undefined variable"?