- Control Structures
- Classes and Objects
Beginner to Intermediate PHP Exercises
Exercises, not Tutorials
The Web is full of excellent PHP tutorials. PHP exercises? Not so much. This site begins to fill that gap with 31 exercises. Choose one from the menu on the left and get started!
How It Works
Come here to test your PHP knowledge by writing scripts as requested. When you are ready, click the button to see the PHP answer script. The answer script output is also available. If you need tutorials, check out one of the PHP tutorial sites listed on the Resources page, then come back and try out what you learned!
The PHP programming exercises progress from the simplest scripts ("Hello world!") to more complex. More difficult requests build on skills included in earlier ones. If you jump ahead and find yourself stuck, go back one or two. Be sure to read the comments in the answer script for some explanation. If you find that a different approach gets the same results, great! Just be sure you understand the PHP syntax the problem was designed to test.
A word about comments
Although we are happy that users participate in the site by leaving comments, exposed answer code below the exercises can defeat the purpose of the site. If you include code in your comments, consider other users and hide it using the syntax highlighter. For detailed instructions, see Commenting Guidelines.
What You'll NeedWe assume that if you are looking for PHP exercises, you already have a server to practice with. If not, check out Your Setup for some tips on getting started.
Featured Exercise - Arrays #7: Multidimensional Array
Multidimensional arrays are arrays that contain arrays. Mastering these is important, as their use often extends beyond the basic uses of arrays.
This PHP exercise has two parts. In the first, you will take the large cities array from Arrays Ex. #5 and transform it into a multi-dimensional array. That will become the data in a table.
Rewrite the large cities array into a multi-dimensional array called $multiCity. The first sub-array will be completely new and include the labels, City, Country, Continent. (These will become the keys in the second half of this exercise.) Each of the succeeding sub-arrays should include those three items, one for each of the cities, for a total of 11 sub-arrays.
Here's the content for your array: City, Country, Continent; Tokyo, Japan, Asia; Mexico City, Mexico, North America; New York City, USA, North America; Mumbai, India, Asia; Seoul, Korea, Asia; Shanghai, China, Asia; Lagos, Nigeria, Africa; Buenos Aires, Argentina, South America; Cairo, Egypt, Africa; London, UK, Europe.