Skip to content

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.

I want toUse this example
Read a CSV file in PHPImport eight sample books with fgetcsv
Decode JSON and catch invalid inputRun the JSON encoding and decoding examples
Query a SQLite databaseCount finished books with PDO
Save a form submission in SQLiteRun the one-file reading list
Read a file next to a PHP scriptFix a working-directory bug
I want toUse this example
Filter an array and return a JSON listKeep a filtered list indexed from zero
Group rows by a fieldRun the multidimensional array examples and look for the groupBy function
Get one column from a list of rowsTry array_column with the reading list
Sort students by average gradeTry the offline PHP Basics exercises
Work with dates and handle invalid inputCompare the exception when parsing a bad date. For arithmetic, try adding a day with DateTimeImmutable
Round and format a number to the nearest hundredCompare number_format results across versions
I want toUse this example
Return JSON from an HTTP endpointBuild and test the simple JSON API
Read and validate form fieldsProcess form input in PHP
Match a URL to a PHP handlerBuild a basic HTTP router