EXERCICES ON PHP PROGRAMMING LANGUAGE
EXERCICES ON PHP PROGRAMMING LANGUAGE
Here’s a cohesive set of exercises on PHP programming, designed to cover a variety of topics
such as basic syntax, control structures, functions, arrays, object-oriented programming, file
handling, and database interaction. These exercises provide comprehensive practice and will
fill an A4 paper when printed.
1. Basics of PHP
2. Conditional Statements
Write a PHP script to check whether a number entered by the user is positive,
negative, or zero using if-else.
Create a script that uses a switch statement to determine the day of the week based on
a numeric input (1 for Monday, 2 for Tuesday, etc.).
Write a PHP script to check if a given year is a leap year using nested if-else.
3. Loops
Write a script to display numbers from 1 to 100, replacing multiples of 3 with "Fizz,"
multiples of 5 with "Buzz," and multiples of both with "FizzBuzz."
Create a script that calculates the factorial of a number using a for loop.
Write a PHP script to find the sum of even numbers between 1 and 50 using a while
loop.
4. Functions
Create a function isPrime($n) that checks whether a given number is prime. Test it
with different values.
Write a function reverseString($str) that takes a string and returns it reversed.
Implement a recursive function to calculate the Fibonacci sequence up to a given
number.
5. Arrays
Write a PHP script that initializes an array of numbers and finds the largest and
smallest numbers in the array.
Create a script that sorts an array of strings alphabetically using the sort() function.
Write a PHP script to merge two arrays and remove duplicate elements.
6. Strings
Write a PHP script to count the number of vowels, consonants, digits, and whitespace
characters in a string.
Create a function that checks if a given string is a palindrome.
Write a script to extract and display the first 5 words of a paragraph using string
functions.
7. Object-Oriented Programming
Create a PHP class called Car with attributes make, model, and year. Add methods
to set and display these attributes. Create objects of the class and use the methods.
Write a script that demonstrates inheritance by defining a base class Animal and a
derived class Dog, with methods like makeSound().
Implement method overloading using PHP magic methods (__call()).
8. File Handling
Write a PHP script to create a text file, write data into it, and read the data back.
Create a script that counts the number of words and lines in a text file.
Write a PHP script to copy the content of one file to another.
9. Database Interaction
Write a PHP script to connect to a MySQL database and create a table users with
fields id, name, email, and password.
Create a script to insert data into the users table, retrieve it, and display it in an
HTML table.
Write a PHP script to delete a specific record from the users table based on the user
ID.
Write a PHP script to start a session, store user information (e.g., name, email), and
display it on another page.
Create a script to demonstrate the use of cookies to store and retrieve the user's
preferred theme (e.g., dark or light mode).
Write a login system using sessions to track user authentication.
Additional Challenges
1. Build a "To-Do List" application where users can add tasks, mark them as complete,
and delete tasks. Use MySQL to store the data.
2. Write a script to simulate a simple contact form that sends user messages to an email
address using PHP's mail() function.
3. Create a small inventory management system using PHP and MySQL, allowing users
to add, update, delete, and view products.
Bonus Challenge
Develop a PHP script that integrates with a public API (e.g., OpenWeatherMap) to
fetch and display weather information based on user input (city name).