Activity 4

Test Yourself with Exercises

1. Create a simple form with two input fields for the user to enter two numbers performs arithmetic operations (addition, subtraction, multiplication, division) on them. The form submits the data to a PHP script using the GET method.

Solution:

if (isset($_GET['first_input']) && isset($_GET['second_input'])) {

      $first_input = $_GET['first_input'];

      $second_input = $_GET['second_input'];

      echo "$first_input";

      echo "$second_input";

      $sum = $first_input + $second_input;

      $difference = $first_input - $second_input;

      $product = $first_input * $second_input;

      if ($second_input != 0) {

          $quotient = $first_input / $second_input;

      } else {

          $quotient = "Cannot divide by zero";

      }

      echo "Sum: $sum";

      echo "Difference: $difference";

      echo "Product: $product<";

      echo "Quotient: $quotient";

      }

      else {

      echo "Invalid Input";

      }

Output:





2. Create a simple form that allows to input a message and counts the number of words in it. The form submits the data to a PHP script using the GET method.

Solution:

if (isset($_GET['message']) && !empty($_GET['message'])) {

        $message = $_GET['message'];

        

        $word_count = str_word_count($message);

        echo "<p>Your message: $message<p>";

        echo "<p>echo "<p>Number of words: $word_count<p>";

     } else {

        echo "<p>No message entered."<p>;

     }

Word Count