PHP All Exercises & Assignments

Practice your PHP skills using PHP Exercises & Assignments. Tutorials Class provides you exercises on PHP basics, variables, operators, loops, forms, and database.

Once you learn PHP, it is important to practice to understand PHP concepts. This will also help you with preparing for PHP Interview Questions.

Here, you will find a list of PHP programs, along with problem description and solution. These programs can also be used as assignments for PHP students.

Write a program to count 5 to 15 using PHP loop

Description:
Write a Program to display count, from 5 to 15 using PHP loop as given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Rules & Hint

  • You can use “for” or “while” loop
  • You can use variable to initialize count
  • You can use html tag for line break

Click below to View Solution/Program


Output:
5
6
7
8
9
10
11
12
13
14
15


Write a factorial program using for loop in php

Description:

Write a program to calculate factorial of a number using for loop in php.

Output:

  The factorial of 3 is 6

Factorial program in PHP using recursive function

Exercise Description:
Write a PHP program to find factorial of a number using recursive function.

What is Recursive Function?

  • A recursive function is a function that calls itself.

Factorial program in PHP using recursive function

Output:

  24

Write a program to create Chess board in PHP using for loop

Description:

Write a PHP program using nested for loop that creates a chess board.

Conditions:

  • You can use html table having width=”400px” and take “30px” as cell height and width for check boxes.

Click to View Solution/Program

Output:
Chess-board-in-PHP-using-for-loop


Write a Program to create given pattern with * using for loop

Description:
Write a Program to create following pattern using for loops:

Rules

  • You can use for or while loop
  • You can use multiple (nested) loop to draw above pattern

Click below to View Solution/Program using two for loops

Output:
*
**
***
****
*****
******
*******
********


Simple Tips for PHP Beginners

When a beginner start PHP programming, he often gets some syntax errors. Sometimes these are small errors but takes a lot of time to fix. This happens when we are not familiar with the basic syntax and do small mistakes in programs. These mistakes can be avoided if you practice more and taking care of small things.

I would like to say that it is never a good idea to become smart and start copying. This will save your time but you would not be able to understand PHP syntax. Rather, Type your program and get friendly with PHP code.

Follow Simple Tips for PHP Beginners to avoid errors in Programming

  • Start with simple & small programs.
  • Type your PHP program code manually. Do not just Copy Paste.
  • Always create a new file for new code and keep backup of old files. This will make it easy to find old programs when needed.
  • Keep your PHP files in organized folders rather than keeping all files in same folder.
  • Use meaningful names for PHP files or folders. Some examples are: syntax-check.php”, “variable-test.php”, “loops.php” etc. Do not just use “abc.php”, “123.php” or “sample.php”
  • Avoid space between file or folder names. Use hyphens (-) instead.
  • Use lower case letters for file or folder names. This will help you make a consistent code

These points are not mandatory but they help you to make consistent and understandable code. Once you practice this for 20 to 30 PHP programs, you can go further with more standards.

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interop Group.


Using phpinfo() – Display PHP Configuration & Modules

phpinfo(); is a PHP built-in function used to display information about PHP’s configuration settings and modules.

When we install PHP, there are many additional modules also get installed. Most of them are enabled and some are disabled. These modules or extensions enhance PHP functionality. For example, the date-time extension provides some ready-made function related to date and time formatting. MySQL modules are integrated to deal with PHP Connections.

It is good to take a look on those extensions. Simply use phpinfo(); function as given below.

Example Using phpinfo() function

Output:
Using-phpinfo-Display-PHP-Configuration-Modules


Write a program to calculate Electricity bill in PHP

Description:

You need to write a PHP program to calculate electricity bill using if-else conditions.

Conditions:

  • For first 50 units – Rs. 3.50/unit
  • For next 100 units – Rs. 4.00/unit
  • For next 100 units – Rs. 5.20/unit
  • For units above 250 – Rs. 6.50/unit
  • You can use conditional statements.

Click to View Solution/Program

Output:

Output of the Electicity Bill Program

calculate-electricity-bill-in-php


Write a simple calculator program in PHP using switch case

Description:

You need to write a simple calculator program in PHP using switch case.

Operations:

  • Addition
  • Subtraction
  • Multiplication
  • Division

Click to View Solution/Program

Output:
simple-calculator-program-in-PHP-using-switch-case


Remove specific element by value from an array in PHP?

Description:

You need to write a program in PHP to remove specific element by value from an array using PHP program.

Instructions:

  • Take an array with list of month names.
  • Take a variable with the name of value to be deleted.
  • You can use PHP array functions or foreach loop.

Solution 1: Using array_search()

With the help of array_search() function, we can remove specific elements from an array.

Output:  array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [3]=> string(5) “april” [4]=> string(3) “may” }


Solution 2: Using foreach()

By using foreach() loop, we can also remove specific elements from an array.

Output:  array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [2]=> string(5) “march” [4]=> string(3) “may” }


Solution 3: Using array_diff()

With the help of array_diff() function, we also can remove specific elements from an array.

Output:  array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [2]=> string(5) “march” [4]=> string(3) “may” }