How to Add two number enter the value on run time by user in PHP using $_Get and $_Post Method

Are you looking for Add two number enter the value on run time by user in PHP or to add two numbers in PHP that are entered by the user at runtime? PHP provides a variety of functions and techniques that can be used to accomplish this task.

One way to add two numbers in PHP is to use the $_GET superglobal array. This array is used to collect data that is passed to the script via the URL. To use this method, you will need to create an HTML form that includes two input fields for the user to enter their numbers.

Here is an example of an HTML form that allows a user to enter two numbers and submit them to a PHP script for addition:

<form method="get" action="add.php">
  Enter first number: <input type="text" name="num1"><br>
  Enter second number: <input type="text" name="num2"><br>
  <input type="submit" value="Add">
</form>

Next, create a PHP script called add.php to process the form submission and add the two numbers. In the script, you can access the numbers that the user entered via the $_GET array using the name attributes of the input fields as keys. For example, to access the first number, you would use $_GET['num1'].

Here is an example of a PHP script that adds the two numbers and displays the result:

<?php
  $num1 = $_GET['num1'];
  $num2 = $_GET['num2'];
  $sum = $num1 + $num2;
  echo "The sum of $num1 and $num2 is $sum.";
?>

You can also use the $_POST superglobal array to collect data from an HTML form and add the numbers in a similar way. The $_POST array is used to collect data that is submitted via an HTTP POST request, rather than via the URL.

To use the $_POST array, you will need to change the method attribute of the form element to “post” and the action attribute to the PHP script that will process the form submission.

<form method="post" action="add.php">
  Enter first number: <input type="text" name="num1"><br>
  Enter second number: <input type="text" name="num2"><br>
  <input type="submit" value="Add">
</form>

Then, in the PHP script, you can access the numbers that the user entered via the $_POST array using the name attributes of the input fields as keys.

<?php
  $num1 = $_POST['num1'];
  $num2 = $_POST['num2'];
  $sum = $num1 + $num2;
  echo "The sum of $num1 and $num2 is $sum.";
?>

You can also use the readline function to prompt the user to enter their numbers at the command line, and the fscanf function to read the user’s input. Here is an example of a PHP script that uses these functions to add two numbers entered by the user at the command line:

<?php
  echo "Enter first number: ";
  $num1 = readline();
  echo "Enter second number: ";
  $num2 = readline();
  $sum = $num1 + $num2;
  echo "The sum of $num1 and $num2 is $sum.";
?>

You can also use the fopen, fgets, and fclose functions to read user input from a file. Here is an example of a PHP script that uses these functions to add two numbers entered by the user in a text file:

<?php
  $file = fopen("numbers.txt", "r");
  $num1 = fgets($file);
  $num2 = fgets($file);
  fclose($file);
  $sum = $num1 + $num2;
  echo "The sum of $num1 and $num2 is $sum.";
?>

In this example, the script reads the first two lines of the numbers.txt file and adds them together.

No matter which method you choose, adding two numbers entered by the user at runtime in PHP is a simple task. With a little bit of code, you can create a user-friendly interface that allows users to enter their numbers and see the result of the addition.

If you need the complete code. Please let me know in the comment section. I will provide the complete code file.

2 thoughts on “How to Add two number enter the value on run time by user in PHP using $_Get and $_Post Method”

  1. I do trust all the ideas you’ve presented in your post. They are really convincing and will definitely work. Nonetheless, the posts are too short for newbies. May just you please lengthen them a bit from next time? Thank you for the post.

    Reply
  2. I have been surfing online more than 2 hours today, yet I
    never found any interesting article like yours. It’s pretty worth enough for
    me. In my opinion, if all webmasters and bloggers made good content as you did, the internet will be a lot more useful than ever before.

    Reply

Leave a Comment

Related Posts