PHP ยท Practice
PHP exercises
14 exercises: 5 easy, 6 medium and 3 hard. Each has a starter file, examples, hints and a solution you can reveal, and your program is checked against every test case. Running PHP in the browser is not available yet: write your program here, then run it locally against the examples.
- Ferry fare totalEasy
Compute the total cost of ferry tickets for adults and children in PHP and print it with two decimal places using sprintf.
BasicsNot started
- Initials from a full nameEasy
Read a full name and print its initials in uppercase. Practise explode, string indexing and strtoupper in PHP.
StringsNot started
- Parcel weights summaryEasy
Read a line of parcel weights into a PHP array and print the total and the heaviest one using explode, array_sum and max.
ArraysNot started
- Platform announcementEasy
Read a platform number and a destination, then print a railway announcement line. A first PHP exercise in reading input and echoing text.
BasicsNot started
- Vaccine fridge statusEasy
Classify a fridge temperature reading as OK, TOO COLD or TOO WARM with if, elseif and else in PHP. Practise comparisons on decimal input.
Control flowNot started
- Dose checker with exceptionsMedium
Validate pharmacy input with InvalidArgumentException in PHP, catch it per line, and keep processing after each error.
ExceptionsNot started
- Fun run results tableMedium
Sort race finishers by time and then by name with usort and the spaceship operator, and print a ranked results table in PHP.
SortingNot started
- Headline to URL slugMedium
Turn a blog headline into a URL slug in PHP: lowercase, replace runs of non-alphanumerics with one hyphen, trim, and handle empty results.
StringsNot started
- Ingredient tally for the weekMedium
Total ingredient quantities across recipes with a PHP associative array, then print them sorted by name using ksort.
Associative arraysNot started
- Postage cost per parcelMedium
Write a PHP function that prices parcels by weight band and express flag, returns integer cents, and prints each cost with two decimals.
FunctionsNot started
- Savings jar commandsMedium
Model a savings jar as a PHP class with a private balance, then drive it with add, take and show commands read from input.
Classes and objectsNot started
- Fewest charging stops for the vanHard
Plan the minimum charging stops for an electric van on a straight route with a greedy choice in PHP, or report that the trip is impossible.
Control flowNot started
- Flatten a nested config fileHard
Recursively flatten a nested JSON object into dotted key=value lines in PHP with json_decode, is_array and a recursive function.
JSON and dataNot started
- Longest dry stretch within a rainfall budgetHard
Find the longest run of consecutive days whose total rainfall stays within a budget. A sliding window (two pointers) exercise in PHP.
ArraysNot started