Write a PHP program to add two numbers

Description: Write a PHP program to find the sum (addition) of two numbers.

Rules and hints:


PHP program to add two numbers

<?php
$first = 15;
$second = 10;

$sum = $first + $second;

echo "Result: " . $sum;
?>

Output:

Tutorials Class - Output Window

Result: 25

How it works

  • The two numbers are stored in $first and $second.
  • The result of the addition is stored in $sum and printed.

Try this too

  • Change the program to subtract, multiply and divide the numbers.
  • Take the numbers from an HTML form using $_POST. See PHP Forms Handling.

Learn more about the similar topics:
Tutorials
PHP Operators
PHP Ternary Operator
Exercises & Assignments
Write a PHP program to add two numbers
Interview Questions & Answers
What is the difference between == and === Operators in PHP?
What are the arithmetic operators in PHP?
What is the use of the ternary operator in PHP?
What is the null coalescing operator (??) in PHP?
What is the difference between division (/) and modulus (%) in PHP?
What is the difference between != and !== in PHP?
What is the difference between $var++ and $var + 1 in PHP?