PHP Exercises

Tutorials Class provides you exercises on various PHP Topics such as PHP basics, variables, operators, loops, forms, and database. This page is an Index for PHP Exercises.

Choose the PHP Exercise type that you want to practice. Practice your PHP skills using PHP Exercises & Assignments.

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 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.


Experiment with Basic PHP Syntax Errors

When you start PHP Programming, you may face some programming errors. These errors stops your program execution. Sometimes you quickly find your solutions while sometimes it may take long time even if there is small mistake. It is important to get familiar with Basic PHP Syntax Errors

Basic Syntax errors occurs when we do not write PHP Code correctly. We cannot avoid all those errors but we can learn from them.

Here is a working PHP Code example to output a simple line.

Output:

 Hello World!

It is better to experiment with PHP Basic code and see what errors happens.

  • Remove semicolon from the end of second line and see what error occurs
  • Remove double quote from “Hello World!” what error occors
  • Remove PHP ending statement “?>” error occurs
  • Use “
  • Try some space between “

Try above changes one at a time and see error. Observe What you did and what error happens.

Take care of the line number mentioned in error message. It will give you hint about the place where there is some mistake in the code.

Read Carefully Error message. Once you will understand the meaning of these basic error messages, you will be able to fix them later on easily.

Note: Most of the time error can be found in previous line instead of actual mentioned line. For example: If your program miss semicolon in line number 6, it will show error in line number 7.


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” }


Write a PHP program to check if a person is eligible to vote

Description:

Write a PHP program to check if a person is eligible to vote or not.

Condition
Click to View Solution/Program.
OUTPUT :You Are Eligible For Vote

Write a PHP program to calculate area of rectangle

Description:

Write a PHP program to calculate area of rectangle by using PHP Function.

Condition

  • You must use a PHP Function.
  • There should be two arguments i.e. length & width.
Click to View Solution/Program.
Output: Area Of Rectangle with length 2 & width 4 is 8 .

Write a PHP program to check whether a number is positive, negative or zero

Description:

Write a PHP program to check whether a number is positive, negative or zero.

Instructions:

  • You can use if else conditions.
  • You should use appropriate PHP Operators.
  • Also check if it not a numeric value.

Click to View Solution/Program