gpt-4-1106-preview· openai
Suzhen/CodeChat
write a php code for addition of two numbers
- User
write a php code for addition of two numbers
- Assistant
Sure, here is a simple PHP script to add two numbers:
<?php // Assign the numbers to variables $number1 = 10; $number2 = 20; // Addition of numbers $sum = $number1 + $number2; // Display the result echo "The sum of {$number1} and {$number2} is {$sum}."; ?>This script initializes two variables with numbers, adds them together, and then displays the result.
You can run this script in a PHP-enabled environment, and it will output:
The sum of 10 and 20 is 30.If you want to make it more dynamic, you can take the numbers from user input, for example, via a form or query parameters:
<?php // Check if the form is submitted if (isset($_GET['submit'])) { // Retrieve numbers from the GET parameters $number1 = isset($_GET['number1']) ? (float)$_GET['number1'] : 0; $number2 = isset($_GET['number2']) ? (float)$_GET['number2'] : 0; // Addition of numbers $sum = $number1 + $number2; // Display the result echo "The sum of {$number1} and {$number2} is {$sum}."; } else { // If the form is not submitted, display the form ?> <form action="" method="get"> Number 1: <input type="text" name="number1"><br> Number 2: <input type="text" name="number2"><br> <input type="submit" name="submit" value="Add"> </form> <?php } ?>