Q:

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

belongs to collection: PHP Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

PHP code:

<?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

Code 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Code to print current date in various formats... >>
<< PHP Code to print text using echo command...