Add Two Numbers in PHP Program



Problem Description

In this problem, we are given two numbers, and we have to find the value obtained after adding one number to another. In this article, we are going to learn how we can add two numbers in PHP using different approaches.

Example 1

Input

$number1 = 8;
$number2 = 12;

Output

20

Explanation

The addition of $number1 + $number2, i.e., 8 + 12, will give 20 as result.

Example 2

Input

$number1 = 10;
$number2 = 10;

Output

20

Explanation

The addition of $number1 + $number2, i.e., 10 + 10, will result in 20.

Below are different approaches for Adding Two Numbers in PHP

There are various approaches to adding two numbers, but here we will discuss three common and widely used methods:

  • Direct Addition
  • Using Functions
  • Using a Class

Direct Addition Using Basic Arithmetic

This is the simple and direct approach for adding two numbers in any programming language. In this approach, we directly add the second number to the first number using the plus (+) operator. This is the simplest approach to understand for beginners as it uses the direct + operator to calculate the sum of two numbers.

Syntax

result = $number1 + $number2;

Implementation Code

<?php

// Define two numbers
$number1 = 10;
$number2 = 4;

// Perform addition
$result = $number1 + $number2;

// Display the result
echo "The result of addition is: " . $result;
?>

Output

The result of addition is: 14

Time Complexity:

O(1)

Space Complexity:

O(1)

Using Functions

In this approach, we use a function that performs addition between two numbers. After adding, it returns the result. This approach is more organized (structured), and reusable, and works well for larger programs.

Implementation Code

<?php

// Function to add two numbers
function addTwoNumbers($a, $b) {
return $a + $b;
}

// Define numbers
$number1 = 15;
$number2 = 9;

// Call function and store result
$result = addTwoNumbers($number1, $number2);

// Display the result
echo "The result of addition is: " . $result;
?>

Output

The result of addition is: 24

Time Complexity:

O(1)

Space Complexity:

O(1)

Using a Class

Classes are a fundamental concept in object-oriented programming (OOP). OOP is a programming paradigm that organizes code into reusable blueprints called classes. These classes are used to create objects. We create a class that encapsulates the addition logic in its method. Finally, we create an object to call the method and pass two numbers as arguments.

Implementation Code

<?php

// Define the Addition class
class Addition {
// Method to perform addition
public function addTwoNumbers($a, $b) {
return $a + $b; // Add and return result
}
}

// Create an object of the Addition class
$obj = new Addition();

// Input numbers
$number1 = 20;
$number2 = 8;

// Call the add method using the object
$result = $obj->addTwoNumbers($number1, $number2);

// Display the result
echo "The result of addition is: " . $result;
?>

Output

The result of addition is: 28

Time Complexity:

O(1)

Space Complexity:

O(1)

Updated on: 2025-02-07T11:19:32+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements