Open In App

PHP Program to Add Two Numbers

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers, the task is to add two numbers in PHP. It is one of the most basic arithmetic operations and a fundamental concept in programming.

Examples:

Input: num1 = 10, num2 = 20
Output: sum = 30

Input: num1 = 70, num2 = 120
Output: sum = 190

Using + operator

The simplest way to add two numbers in PHP is by using variables to store the numbers and then adding them using the “+” operator.

Example: This function illustrates the above mentioned approach to Add two numbers.

PHP
<?php

// Define two numbers
$num1 = 10;
$num2 = 20;

// Add the numbers
$sum = $num1 + $num2;

// Display the result
echo "The sum of $num1 and $num2 is: $sum";

?>

Output
The sum of 10 and 20 is: 30

Using Arrays and Array Functions

Another approach to add two numbers in PHP is by using arrays and array functions. This method is particularly useful when you want to add more than two numbers or when the numbers are already stored in an array.

  • array_sum($nums) calculates the sum of the elements in the $nums array.
  • implode(” and “, $nums) creates a string from the $nums array elements, separated by ” and “, for display purposes.

Example: This function illustrates the above mentioned approach to Add two numbers.

PHP
<?php

// Define an array containing 
// the numbers to be added
$nums = [10, 20];

// Use array_sum() to add the numbers
$sum = array_sum($nums);

// Display the result
echo "The sum of " . implode(" and ", $nums) . " is: $sum";

?>

Output
The sum of 10 and 20 is: 30

Using Bitwise Operator

We will use Bitwise Operator to add two numbers. First, we check num2 is not equal to zero, then Calculate the carry using & operator, and then use XOR operator to get sum of numbers. At last, shift the carry to 1 left.

  • Define a function add($num1, $num2) that takes two integers as input and returns their sum.
  • Inside the function, a while loop is used to iterate until there is no carry left. The loop continues as long as $num2 (the carry) is not equal to 0.
  • The carry is calculated using the AND operator ($carry = $num1 & $num2). This operation identifies the bits that will produce a carry.
  • The sum of bits of $num1 and $num2 where at least one of the bits is not set is calculated using the XOR operator ($num1 = $num1 ^ $num2). This operation performs the addition without considering the carry.
  • The carry is then shifted to the left by one position ($carry << 1) to align it with the next higher bit position for the next iteration.
  • The loop continues until there is no carry, at which point $num1 contains the final sum.
  • The function returns the sum of the two numbers.

Example: This function illustrates the above mentioned approach to Add two numbers.

PHP
<?php

function add($num1, $num2) {
    while ($num2 != 0) {
        $carry = $num1 & $num2;
        $num1 = $num1 ^ $num2;
        $num2 = $carry << 1;
    }
    
    return $num1;
}

// Given data
$num1 = 15;
$num2 = 32;

$sum = add($num1, $num2);
// Displaying the result
echo "Sum of $num1 and $num2: $sum";

?>

Output
Sum of 15 and 32: 47

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads