Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
PHP program to calculate sum of two numbers using function

PHP program to calculate sum of two numbers using function

Posted on July 1, 2022July 7, 2022

Table of Contents

  • PHP program to calculate sum of two numbers using function
    • Code to sum  of two numbers in PHP
      •  Addition of two numbers using function  – #1
      • Addition of two numbers using function – #2

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes