Practical PHP Examples by Task
Choose something you want PHP to do. Each example leads to runnable code or a lesson with commands and expected output.
Files and data
Section titled “Files and data”| I want to | Use this example |
|---|---|
| Read a CSV file in PHP | Import eight sample books with fgetcsv |
| Decode JSON and catch invalid input | Run the JSON encoding and decoding examples |
| Query a SQLite database | Count finished books with PDO |
| Save a form submission in SQLite | Run the one-file reading list |
| Read a file next to a PHP script | Fix a working-directory bug |
Arrays, numbers, and dates
Section titled “Arrays, numbers, and dates”| I want to | Use this example |
|---|---|
| Filter an array and return a JSON list | Keep a filtered list indexed from zero |
| Group rows by a field | Run the multidimensional array examples and look for the groupBy function |
| Get one column from a list of rows | Try array_column with the reading list |
| Sort students by average grade | Try the offline PHP Basics exercises |
| Work with dates and handle invalid input | Compare the exception when parsing a bad date. For arithmetic, try adding a day with DateTimeImmutable |
| Round and format a number to the nearest hundred | Compare number_format results across versions |
Web responses
Section titled “Web responses”| I want to | Use this example |
|---|---|
| Return JSON from an HTTP endpoint | Build and test the simple JSON API |
| Read and validate form fields | Process form input in PHP |
| Match a URL to a PHP handler | Build a basic HTTP router |
Practise offline
Section titled “Practise offline”- PHP Basics offline exercises gives you starter files, sample inputs, and solutions.
- PHP debugging challenges lets you fix four short problems.
- Printable PHP array reference summarises common operations on one page.
- Sample reading list dataset gives you the same records in CSV, JSON, and SQLite.
- PHP behaviour across versions gives you three small examples to run on your own PHP installation.