PHP program to create a function to find the addition of two integer numbers

Here, we are going to implement a PHP program/code to find the sum/addition of two numbers using a user-defined function.
Submitted by Bhanu Sharma, on August 08, 2019 [Last updated : March 12, 2023]

Addition of two integer numbers

Given two numbers and we have to find their addition/sum in PHP using a user-defined function.

PHP code to find the addition of two integer numbers

<?php
//function definition
//this function will take two integer arguments
//and return the sum of them
function addTwoNumbers(int $x, int $y)
{
    return $x + $y;
}

//calling the function and printing the result
echo " sum of 10 and 25 : " . addTwoNumbers(10, 25);
echo "<br>";
echo " sum of 30 and -10: " . addTwoNumbers(30, -10);
echo "<br>";
?>

Output

 sum of 10 and 25 : 35
 sum of 30 and -10: 20

Explanation

The function works by using the standard math operation of adding two integers. Whenever we need to call the function, we use the following format addTwoNumbers (x, y); by replacing the values of x and y with two valid integers, we get the resulting sum in the output.

PHP Basic Programs »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.