Exercises in PHP
Sure! Here are some PHP exercises—ranging from beginner to intermediate—to help you practice and master core concepts like variables, arrays, loops, functions, forms, file handling, and more.
? Beginner Exercises
1. Print "Hello, World!"
<?php// TODO: Print Hello, World!?>2. Add Two Numbers
<?php// TODO: Create two variables and print their sum?>3. Check Even or Odd
<?php// TODO: Check if a number is even or odd?>4. Simple Calculator
<?php// TODO: Use if or switch to perform +, -, *, / on two numbers?>5. Use of if, elseif, else
<?php// TODO: Grade a student using marks (90+ A, 80+ B, etc.)?>6. Print 1 to 10 Using Loop
<?php// TODO: Use a for or while loop to print numbers 1 to 10?>7. Create an Array and Loop Through It
<?php// TODO: Create an array of fruits and print each using foreach?>? Intermediate Exercises
8. Factorial of a Number
<?php// TODO: Write a function to find the factorial of a number?>9. Reverse a String
<?php// TODO: Write a function to reverse a given string?>10. Palindrome Checker
<?php// TODO: Check if a word is a palindrome (same forwards and backwards)?>11. Form Handling - Greet User
<!-- index.php --><form method="post" action="greet.php"> Enter your name: <input type="text" name="name"> <input type="submit"></form><!-- greet.php --><?php// TODO: Get the name using $_POST and greet the user?>12. Find Prime Numbers from 1 to 100
<?php// TODO: Loop from 1 to 100 and print prime numbers only?>13. Create a Simple Login Form
HTML form with username and password.
Validate using PHP and hardcoded credentials.
14. File Upload System
Create a form to upload files.
Save the file to a directory and display the file name.
15. Session Example
Start a session.
Store a username.
Access it across two pages.
? Advanced Ideas
Build a simple To-Do List using PHP and MySQL.
Make a CRUD (Create, Read, Update, Delete) app.
Implement AJAX Live Search using PHP + jQuery.
Create a simple Blog system (users can post, read, delete).