calculations

PHP program to calculate sum of two numbers using function

PHP program to calculate sum of two numbers using function

In this article, we will discuss the concept of PHP program to calculate sum of two numbers using function

In this post, we are going to learn how to write a program to find  addition of two numbers using function in PHP language

Code to sum  of two numbers in PHP

Addition of two numbers using function  – #1

In this code, we are going to learn how to write a program to calculate addition of two numbers using function in PHP  programming language

Program 1

  • Start PHP script
  • Declare and initiates two integer variable
  • Calculate sum of given two numbers
  • Display result on the screen
  • Exit PHP

In this program, first the user declare variables as parameter in function and then passing the values to variables as argument. Finally when the user call the function, the result is calculated and displayed on the screen

<?php
//function definition
function addTwoNumbers($x, $y)
{
  return $x + $y;
}
//Calling the function and print result
echo "Sum of 20 and 30: ".addTwoNumbers(20,30);
echo "<br>";
echo "Sum of 150 and 550: ".addTwoNumbers(150,550);
echo "<br>";
echo "Sum of 100 and -40: ".addTwoNumbers(100,-40);
echo "<br>";
?>

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

Sum of 20 and 30: 50
Sum of 150 and 550: 700
Sum of 100 and -40: 60

 

Addition of two numbers using function – #2

In this code, we are going to learn how to write a program to calculate addition of two numbers using function in PHP  programming language

Program 2

  • Start PHP script
  • Declare and initiates two integer variable
  • Calculate sum of given two numbers
  • Display result on the screen
  • Exit PHP

In this program, first the user declare variables as parameter in function and then passing the values to variables as argument. Finally when the user call the function, the result is calculated and displayed on the screen

<?php
//function definition
function addTwoNumbers($x, $y)
{
  $sum=$x + $y;
  return $sum;
  echo $sum;
}
//Calling the function and print result
echo "Sum of 100 and 300: ";
echo addTwoNumbers(100,300);
echo "<br>";
echo "Sum of 100 and 120: ";
echo addTwoNumbers(100,120);
echo "<br>";
echo "Sum of 100 and -200: ";
echo addTwoNumbers(100,-200);
echo "<br>";
?>

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

Sum of 100 and 300: 400
Sum of 100 and 120: 220
Sum of 100 and -200: -100

 

Suggested for you

Java program to add two numbers

C program to add two numbers

C++ program to add two numbers

Python program to add two numbers

 

Write a program to sum of prime numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

 

Write a program to sum of natural numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.