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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes