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
phpcommand line. The first run downloads about 7 MB; after that it's instant. Change the examples and run them again.
Your first program
- PHP code lives between
<?phpand (optionally)?>. Anything outside those tags is sent out as-is, which is how PHP mixes code into HTML. echoprints text."\n"is a newline.- Every statement ends with a semicolon.
| 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:
<?= … ?>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
Errors
PHP tells you the problem, the file and the line:
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
- Install PHP 8.3 or newer:
- macOS:
brew install php - Ubuntu/Debian:
sudo apt install php-cli(or theondrej/phpPPA 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
- macOS:
- Check:
php -v - Run a script:
php hello.php - Try code interactively:
php -a - Serve a folder as a website, with PHP's built-in server:
php -S localhost:8000, then open http://localhost:8000
Practice
- Print your name, city and age in one sentence with variables inside double quotes.
- Use
printfto print a price with exactly two decimal places. - Use
var_dumpon a number, a string,trueandnulland compare the output. - Build a small HTML table of three products with
foreach.
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"?