addition

PHP program to Addition of Two Numbers using function – PHP program

PHP program to Addition of Two Numbers using function – PHP program

In this tutorial, we will discuss the concept of PHP program to Addition of Two Numbers using function – PHP program

In this topic, we are going to learn how  to write a program to add two numbers using user-defined function in  PHP programming language

Addition of Two Numbers using function

Sum of two numbers

Sum of two numbers using function – #1

In this program, the user declare two integer variables $x and $y as parameters of the function. Then two numbers are added using plus(+) operator. finally when the user call the function, the result is displayed on the screen

Program 1

  • function definition
  • Return value to main
  • Calling the function and print result on the screen
<?php
function addTwoNumbers($x, $y)
{
    
    return $x + $y;
}

echo "Sum of 10 and 20: ".addTwoNumbers(10,20);
echo "<br>";
echo "Sum of 100 and 205: ".addTwoNumbers(100,205);
echo "<br>";
echo "Sum of 100 and -20: ".addTwoNumbers(100,-20);
echo "<br>";
?>

When the above code is executed, it produces the following result

Sum of 10 and 20: 30
Sum of 100 and 205: 305
Sum of 100 and -20: 80

Sum of two numbers using function – #2

In this program, the user declare two integer variables $x and $y as parameter of the function. Then two numbers are added using plus(+) operator. Finally when the user call the function, the result is displayed on the screen

  • Function definition
  • Return the value to main
  • Calling the function and print result

Program 2

<?php

function addTwoNumbers($x, $y)
{
    $sum=$x + $y;
    return $sum;
    echo $sum;
}

echo "Sum of 10 and 30: ";
echo addTwoNumbers(10,30);
echo "<br>";
echo "Sum of 100 and 205: ";
echo addTwoNumbers(100,205);
echo "<br>";
echo "Sum of 100 and -20: ";
echo addTwoNumbers(100,-20);
echo "<br>";
?>

When the above code is executed, it produces the following result

Sum of 10 and 30: 40
Sum of 50 and 20: 70
Sum of 80 and -20: 60

 

Similar post

Sum of two numbers in C language

Sum of two numbers in C++ language

Sum of two numbers in Python language

Sum of two numbers in Java language without + operator

Sum of two numbers in Java language using bitwise operator

Sum of two numbers in C language without + operator

Sum of two numbers in C language using bitwise operator

 

Calculate addition of two numbers using method in Java language

Calculate addition of two numbers using function in C language

Calculate addition of two numbers using function in C++ language

Calculate addition of two numbers using function in Python language

 

Operator in Java language

Operator in C language

Operator in C++ language

Operator in Python language

 

PHP Adding Two Numbers  - PHP program
PHP subtracting two numbers-PHP program
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago