Skip to content

Exercise path · PHP

10 PHP exercises, in order

From your first line to a complete program you assembled yourself. Each exercise introduces exactly one new idea, runs in this tab, and tells you in plain language what happened.

How the path works

You never start from a blank editor. Every exercise hands you a program that already runs and asks you to change or add one thing, so the first success is minutes away and the last exercise is a program you can read top to bottom and explain. The ideas arrive in this order: output, variables, arithmetic, conditions, loops, loops and conditions, strings and formatting, arrays, putting it together, a complete program.

Run is the check. There is no separate submit button: press Run, and the output is compared with what the exercise asked for. When it is not right, the feedback is a sentence about what happened — the line that differed, or what the error means — rather than a verdict. Hints come one at a time, and the answer opens only after you have run your code at least once, because reading the answer first is how you learn nothing.

Your finished exercises and the code you last had in the editor are saved in this browser, so a half-done exercise is still there tomorrow. Nothing is uploaded and there is no account.

The exercises

  1. First Lines of PHPTwo lines of welcome text printed to the screen, from code you repaired yourself.New idea: echo prints text, and every statement ends with a semicolon
  2. Greet By NameA personalized greeting built by combining text with a variable, instead of typing the name straight into the string.New idea: a variable stores a value under a name starting with $
  3. Ticket TotalA small receipt that multiplies a ticket price by a quantity and prints the total cost.New idea: arithmetic operators (+ - * /) and joining text with .
  4. Open or ClosedA message that changes depending on the hour stored in a variable — 'open' or 'closed' — decided by the program itself.New idea: if/else with && requiring every part of a condition to be true
  5. Launch CountdownA countdown from 5 down to 1, printed one number per line, followed by a liftoff message.New idea: a for loop repeats a block a set number of times
  6. Even Numbers OnlyA program that walks through the numbers 1 to 10 and prints only the even ones, using % to test each number as the loop reaches it.New idea: an if inside a for loop, decided again on every pass
  7. Score ReportA neatly formatted line of text with a player's ID, name and score lined up using sprintf(), instead of stitched together with dots.New idea: sprintf() builds a formatted string from a template
  8. Packing ListA packing list printed one item per line from an array, using foreach instead of a separate echo for every item.New idea: an array holds several values, and foreach walks through them
  9. Total WeightThe combined weight of every item in a packing list, added up one item at a time inside a loop.New idea: a variable before a loop can carry a running total
  10. Trip SummaryA trip packing summary that reads a traveler's name from input, walks a list of item weights, flags anything over a limit, and prints a formatted total — combining everything from the earlier exercises into one program.New idea: assembling a complete program from parts you already know

After the path

The PHP playground is where to build something of your own, and the PHP guide sets out a learning order for everything after the basics. Everything you can practise PHP with on this site is gathered on the PHP practice hub. The same path also exists in Python, JavaScript, TypeScript, Java, C++, C, C#, Go, Ruby, SQL, R and Bash & the Shell.