30 PHP Basic Exercises for Intermediate with Solutions
Master intermediate PHP skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in PHP, setting a solid foundation for advanced challenges. Start your journey to PHP mastery today!
Learning Objectives:
Enhance your skills by working with sessions, cookies, file handling, databases (MySQL), and object-oriented programming (OOP).
Exercise Instructions:
- Start with the first exercise and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you in more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Write a PHP script to check if a number is a perfect square.
Required Input:
16
Expected Output:
true
Code In Php
Run Code?
Click Run Button to view compiled output
2. Create a PHP script to find the sum of all digits in a given number.
Required Input:
1234
Expected Output:
10
Code In Php
Run Code?
Click Run Button to view compiled output
3. Write a PHP script to convert a number to its Roman numeral equivalent.
Required Input:
9
Expected Output:
IX
Code In Php
Run Code?
Click Run Button to view compiled output
4. Create a PHP script to remove duplicate elements from an array.
Required Input:
[1, 2, 2, 3, 4, 4]
Expected Output:
[1,2,3,4]
Code In Php
Run Code?
Click Run Button to view compiled output
5. Write a PHP script to count the number of words in a sentence.
Required Input:
"Hello world!"
Expected Output:
2
Code In Php
Run Code?
Click Run Button to view compiled output
6. Create a PHP script to implement the bubble sort algorithm.
Required Input:
[3, 1, 4, 2]
Expected Output:
[1,2,3,4]
Code In Php
Run Code?
Click Run Button to view compiled output
7. Write a PHP script to implement selection sort on an array.
Required Input:
[3, 1, 4, 2]
Expected Output:
[1,2,3,4]
Code In Php
Run Code?
Click Run Button to view compiled output
8. Create a PHP script to count the frequency of each word in a string.
Required Input:
"hello world hello"
Expected Output:
{"hello":2,"world":1}
Code In Php
Run Code?
Click Run Button to view compiled output
9. Write a PHP script to reverse the words in a sentence while keeping their order.
Required Input:
"Hello world"
Expected Output:
world Hello
Code In Php
Run Code?
Click Run Button to view compiled output
10. Create a PHP script to generate and display Pascal’s Triangle up to n rows.
Required Input:
4
Expected Output:
[[1],[1,1],[1,2,1],[1,3,3,1]]
Code In Php
Run Code?
Click Run Button to view compiled output