PHP Exercises

Welcome to PHP Exercises at The Coding College! Practice your PHP coding skills with hands-on exercises that cover various levels, from beginner to advanced. Solving these exercises will improve your understanding of PHP syntax, concepts, and real-world applications.

Beginner PHP Exercises

1. Hello World!

Write a simple PHP script to display “Hello, World!” on the screen.

<?php
// Your code here
?>

2. Variables

Create variables $name (string) and $age (integer). Print the values in the following format:

My name is John and I am 25 years old.

3. Simple Addition

Write a PHP script that adds two numbers, $a = 5 and $b = 10, and displays the result.

4. Even or Odd

Write a PHP script that checks whether a given number is even or odd. Example input:

$number = 7;

5. Display Current Date and Time

Write a PHP script to display the current date and time in the format:

Today is: YYYY-MM-DD  
Current time is: HH:MM:SS

6. Create an Array

Create an array with three favorite fruits. Print the second item in the array.

7. Sum of Array Elements

Write a PHP script to calculate the sum of all values in the following array:

$numbers = [2, 4, 6, 8, 10];

Intermediate PHP Exercises

8. Factorial of a Number

Write a function in PHP to calculate the factorial of a given number. Example:

factorial(5); // Output: 120

9. String Reverse

Write a PHP script to reverse a string. Example input:

$string = "Hello, World!";

10. Check Prime Number

Write a PHP script to check if a number is prime. Example:

$number = 13; // Output: Prime Number

11. Create and Sort an Associative Array

Create an associative array with student names as keys and their scores as values. Sort the array in descending order of scores.

12. Find Largest Number

Write a PHP script to find the largest number in an array:

$numbers = [12, 45, 7, 89, 34];

13. String Palindrome

Write a PHP script to check if a string is a palindrome. Example input:

$string = "madam";

14. Form Handling

Create a simple HTML form with fields Name and Email. Write PHP to capture and display the submitted data.

15. Multiplication Table

Write a PHP script to display the multiplication table for numbers 1 to 5.

Advanced PHP Exercises

16. File Handling

Create a PHP script to:

  1. Open a file called example.txt.
  2. Write some content to it.
  3. Read the content back and display it.

17. User Login System

Create a simple login system with a hardcoded username and password. Example:

$username = "admin";
$password = "12345";

18. Database Connection

Write a PHP script to connect to a MySQL database using mysqli_connect() and display a success message.

19. CRUD Operations

Using MySQL and PHP, perform the following operations:

  1. Create a new database table.
  2. Insert a record.
  3. Retrieve all records.
  4. Update a record.
  5. Delete a record.

20. Validate User Input

Create a form where users enter their name, email, and age. Use PHP to:

  • Validate the email format.
  • Ensure the age is a positive number.
  • Display appropriate error messages if validation fails.

Solutions

If you’re stuck or want to verify your solutions, explore detailed answers and explanations on our website:

Next Steps

  • Practice consistently to build your PHP programming skills.
  • Try combining exercises to create small projects (e.g., a To-Do List App or a Contact Form Handler).
  • Check out our PHP Tutorials for concepts, examples, and more exercises.

Leave a Comment