10 PHP Code Challenges for Beginners
You can only fill your head with so much coding theory and knowledge from courses and books before you need to put those skills to the test. If you’re a PHP developer, one way to practice your skills is by building a website or web application. But, there’s a much simpler and less time-consuming way to practice PHP coding.
Code challenges are bite-sized development problems that you can solve with code. By solving code challenges, you’ll build on your existing skills and learn new ones that’ll help you efficiently solve more complex projects. And, with over 75% of websites using PHP today, adding this in-demand skill to your skill set is a no-brainer.
In this article, we’ll give you 10 beginner-level PHP code challenges that are a great start for practicing your PHP skills. While these challenges are suitable for beginners, you may want to brush up on your PHP knowledge before getting started. Check out Learn PHP for a quick refresher, then move on to the challenges below.
1. Create a hello world page
For this challenge, create a PHP page with standard HTML, including head, title, and body tags. Since one feature of PHP is the ability to embed it in HTML, this will give you some practice doing that. Inside the body tag, create an H1 tag that contains a PHP section that prints out “Hello World.”
2. Creating variables
In this challenge, make another PHP page with standard HTML. This time, you’ll want to print “Mary had a little lamb” in the HTML element of your choice. But first you should create a variable to hold “Mary” and a variable to hold “lamb” and use those variables to create the string you print out.
3. Modifying data types
Here, you’ll create one variable and then assign it different values. Then, you’ll print those values to the browser using an echo statement. This statement can only include the words “The value is a”. Print each statement on a new line using line breaks. Your result should look like this:
The value is a string. The value is a double. The value is a boolean. The value is a integer. The value is a NULL.
4. Using if, else, and elseif
For this challenge, write a script that gets the current month and then prints out the season using if, else, and elseif. You don’t have to worry about the exact date of the season switch. Count June, July, and August as summer, and September, October, and November as fall. Count December, January, and February as winter, and March, April, and May as spring.
5. Create a multiplication table using nested for loops
For this challenge, you’ll print a multiplication table to the browser for all numbers up to and including 12. To do this, use two for loops, one nested inside of the other. Make sure you use table, td, and tr HTML tags to create a valid HTML table.
6. Responding to a PHP form
You’ll create two PHP files in this challenge. The first file will contain a form with one form field for a user to enter their name and a button to submit the form. This form should use the POST method and post the results of the form to the second PHP file.
The second file should process the form data and print a greeting that looks like this, if the user entered “Bob” as the name: “Hi, your name is Bob”.
7. Looping an array
In this challenge, first, create an array that contains the following city names:
Tokyo, Mexico City, New York City, Mumbai, Seoul, Shanghai, Lagos, Buenos Aires, Cairo, London
Print the values to the browser separated by commas using a loop to iterate over the array. Sort the array and then print the values to the browser again using a loop. Next, add these city names to the array:
Los Angeles, Calcutta, Osaka, Beijing
Then sort the array again and print the values using a for loop.
8. Write a function that accepts arguments
This challenge involves creating a function in PHP that accepts two numeric arguments. These arguments represent the height and width of a rectangle in inches.
In the function, calculate the area of the rectangle and return a sentence. If you pass the function parameters of 4 and 5, it should return, “This rectangle is 4 inches high and 5 inches wide and has an area of 20 square inches.”
9. Form and function
In this challenge, you’ll use the function that you created in the last challenge. Create two PHP files. One will have a form with two fields for width and height and a submit button. The form should use the POST method and submit to the second PHP file, which will output HTML and the result of the function in an H1 tag.
10. Get environment information
For this challenge, write a script in PHP that prints out the PHP version and configuration values, the client’s IP address, and the browser being used.
Build your PHP skills
If you enjoyed these PHP code challenges, you can access more on the Codecademy Go app. Need a refresher before you dive into more challenges? Don’t forget to check out our Learn PHP course, which is great for new and intermediate PHP developers.
Start learning this in-demand skill with us and sign up for free today.