Adding two numbers in PHP is one of the very first things every beginner should learn. It may sound simple, but this small idea teaches you how PHP handles numbers, variables, and basic logic. Once you understand addition, many other topics start to make sense more easily.
PHP uses addition in many everyday situations. It is used to calculate totals in shopping carts, count users, add scores, and work with money. If we are building a small website to track school results, addition helps combine marks correctly. Learning how to add numbers in PHP the traditional way gives you a strong base that will help you later when programs become bigger.
Program 1: Adding Two Integers in PHP
This first program shows how to add two whole numbers in PHP. It uses predefined integer values and prints the result in the browser.
<?php
$firstNumber = 10;
$secondNumber = 20;
$sum = $firstNumber + $secondNumber;
echo "The sum is " . $sum;
?>PHP stores both numbers as integers and adds them using the plus symbol. The result is saved in a new variable and displayed using echo. This example is useful because integers are common in counting and simple calculations, making it easy for beginners to understand.
Program 2: Adding Two Decimal Numbers in PHP
This program focuses on adding numbers with decimal points. This is common when working with prices or measurements.
<?php
$priceOne = 12.50;
$priceTwo = 7.25;
$totalPrice = $priceOne + $priceTwo;
echo "The total price is " . $totalPrice;
?>PHP automatically understands these values as decimal numbers. The addition works the same way as integers, which helps beginners feel comfortable. This approach is very useful in real websites that deal with money or averages.
Program 3: Adding an Integer and a Decimal Number
Sometimes you need to add a whole number and a decimal number together. PHP handles this smoothly without extra conversion.
<?php
$items = 5;
$extraWeight = 2.5;
$totalWeight = $items + $extraWeight;
echo "The total weight is " . $totalWeight;
?>PHP automatically converts the integer into a decimal during the calculation. This shows beginners how flexible PHP is compared to some other languages. It makes coding faster and easier, especially for simple math.
Program 4: Adding Two Numbers Using a Function
This program shows how addition can be written inside a function. Functions help keep code neat and reusable.
<?php
function addNumbers($first, $second) {
return $first + $second;
}
$result = addNumbers(15, 25);
echo "The result is " . $result;
?>The function receives two numbers, adds them, and returns the result. This is useful when the same calculation is needed many times. Beginners learn a clean and traditional way of organizing logic, which is a good habit in PHP development.
Program 5: Adding Two Numbers from User Input
This example shows how to add numbers entered by a user using the classic fscanf style. This is the traditional way PHP programs handled user input before web forms became common.
<?php
echo "Enter the first number: ";
fscanf(STDIN, "%d", $firstNumber);
echo "Enter the second number: ";
fscanf(STDIN, "%d", $secondNumber);
$sum = $firstNumber + $secondNumber;
echo "The sum is " . $sum;
?>PHP reads the values typed by the user from the keyboard using fscanf. The + operator is used to add the numbers together. This simple example helps beginners learn core PHP input and math, the old and trusted way, before moving on to real web pages.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about adding two numbers in PHP. These are things learners often ask when starting out.
Q1. Does PHP treat numbers as text or real numbers?
PHP automatically understands numbers when they are written normally. When numbers come from forms, PHP converts them during calculations.
Q2. Can PHP add very large numbers?
Yes, PHP can handle large numbers, but extremely large values may need special handling. For beginners, normal numbers work perfectly.
Q3. What happens if I add text instead of numbers?
PHP tries to convert text into numbers. If it cannot, it usually treats the value as zero, which is why validation is important.
Q4. Is addition slow in PHP?
No, addition is very fast. PHP handles simple math operations efficiently even on busy websites.
Conclusion
Adding two numbers in PHP is simple, but it is a powerful skill. In this guide, you learned how to add integers, decimals, mixed values, use functions, and accept user input. These examples follow classic PHP practices that have worked well for many years.
The best way to improve is to practice. Change the numbers, try new examples, and experiment with your own ideas. With steady practice, addition in PHP will feel natural, and your confidence as a programmer will grow step by step.




