PHP Playground

A starter script to experiment with. Edit it here, press Copy, paste it into a file called main.php, and run it with php main.php. Lesson 2 shows how to install PHP.

Try it yourself · main.php
Edit the code, copy it into main.php, then run it with php main.php (PHP 8.5). How to install PHP
Output
Backpack     1 x  39.90 =   39.90
Notebook     4 x   3.50 =   14.00
Pen         10 x   1.20 =   12.00
Total: 65.90

Ideas to try

  • Apply a 10% discount when the total is over 50 (lesson 12).
  • Read the items from a CSV file with fgetcsv (lesson 54).
  • Group items into "cheap" and "expensive" with array_filter (lesson 23).
  • Reject negative prices in the constructor with an exception (lessons 34 and 44).
  • Turn it into a web page: save it as basket.php, start php -S localhost:8000, and print an HTML table instead (lesson 4).
  • Save the basket to a SQLite database with PDO (lesson 57).

Running PHP without installing anything

Online sandboxes such as 3v4l.org run PHP snippets in your browser, against many PHP versions at once. They're great for quick experiments; for forms, sessions and databases you'll want a local installation.