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

View Solution/Program

<?php
$number = 324; // enter any number of your choice here
if ($number > 0) // condition for positive numbers
{
    echo $number . " is a positive number";
} else if ($number < 0) // condition for negative number
{
    echo $number . " is a negative number ";
} else if ($number == 0) // condition for zero
{
    echo "You have entered zero";
} else {
    echo " please enter a numeric value";
}
?>
Tutorials Class - Output Window

324 is a positive number

Write a PHP program to reverse the string

Description:
Write a PHP program to reverse the string.

Instructions:

  • You can use any string related built-in Function.
  • You can use only one variable.

View Solution/Program:

<?php
$str = "Tutorials Class";
echo strrev($str);
?>
Tutorials Class - Output Window

ssalC slairotuT

Write a PHP program to find the length of the string

Description:
Write a PHP program to find the length of the string.

Instructions:

  • You have to use one variable.
  • You must use a built-in PHP String Function.

View Solution/Program:

<?php
$str = "Tutorials Class";
echo strlen($str);
?>
Tutorials Class - Output Window

15

Write a PHP program to count the words in the string

Description:
Write a PHP program to count the words in the string.

Instructions:

  • You can use only one variable.
  • You can use PHP built-in String Function.
View Solution/Program
<?php
$sample_words = "This is Tutorials Class, learn programming tutorials here.";
echo str_word_count($sample_words);
?>
Tutorials Class - Output Window

8

Write a PHP program to convert a string into uppercase

Description:
Write a PHP program to convert all the characters inside the string into uppercase.

Instructions:

  • You can use only one variable.
  • You should use a PHP built-in string Function
View Solution/Program
<?php
$str = "Tutorials Class";
echo strtoupper($str);
?>
Tutorials Class - Output Window

TUTORIALS CLASS

PHP Array to String Conversion (favourite colours chosen by user)

Description:
Write an exercise for PHP Array to String Conversion.

  • Create a form that accept name as well as colors
  • After submission, form data will be sent to another page
  • Display select colors (as a list) and user name
  • User name & colors selection is mandatory

View Solution/Program

<html>
<head>
<title>
array to string
</title>
</head>
<body>
	<form action="data.php" method="post">
		Username: <input type="text" name="username" placeholder="enter name" required/><br/><br/> 
		Select your favourite colors:<br/>
		Red<input type="checkbox" name="check_list[]" value="red"/><br/>
		Blue<input type="checkbox" name="check_list[]" value="blue"/><br/>
		Green<input type="checkbox" name="check_list[]" value="green"/><br/>
		Yellow<input type="checkbox" name="check_list[]" value="yellow"/><br/>
		Pink<input type="checkbox" name="check_list[]" value="pink"/><br/>
		Black<input type="checkbox" name="check_list[]" value="black"/><br/>
		White<input type="checkbox" name="check_list[]" value="white"/><br/><br/>
		<input type="submit" name="submit" value="Submit"/><br/>
	</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
    if (!empty($_POST['check_list'])) {
        // Counting number of checked checkboxes.
        $checked_count = count($_POST['check_list']);
        $name = $_POST['username'];
        echo $name . " 's favourite colors are " . $checked_count . " option(s): <br/>";
        // Loop to store and display values of individual checked checkbox.
        foreach ($_POST['check_list'] as $selected) {
            echo "<p>" . $selected . "</p>";
        }
    } else {
        echo "<b>Please Select Atleast One Option.</b>";
    }
}

How to check if an array is a subset of another in PHP?

Description:

You need to find whether an array is subset of another array.

  • Let us suppose that there are two arrays.
  • First array is large which have 6 values.
  • Second array is small which have 2 values
  • Find if second array is subset of first which means that all values of second array should exists in first array.
  • You can use Decision Making Statements.

PHP Program to find if an array is a subset of another:

<?php
// Define two array
$array1 = array('a','1','2','3','4');
$array2 = array('a','3');

// intersect/matched values should be equal to first array
if (array_intersect($array2, $array1) === $array2) {
  echo "It is a subset";
} else {
   echo "Not a subset";
}
?>
Tutorials Class - Output Window

It is a subset