Table of Contents
PHP multiply two numbers using function
In this article, we will discuss the concept of PHP multiply two numbers using function
In this post, we are going to learn how to write a program to find multiplication of two numbers using function in PHP language
Code to find product of two numbers in PHP
Product of two numbers using function – #1
Program 1
- Start PHP script
- Declare two integer variable as parameter
- Calculate product of given two numbers and return the value to main
- Calling the function with passing argument
- Display result on the screen
- Exit PHP
In this program, first the user declare variables as parameter in function. Then when the user call the function, passing the values to variables as argument. finally , the result is calculated and display on the screen
<html> <head> <title> product of two numbers</title> </head> <body> <h3>Multiply two number using function</h3> <?php //php script //function definition with parameter function mulTwoNumbers($x, $y) { return $x * $y; //calculate the product of two numbers using function //and return the product of given number } //Calling the function with argument to print result echo "The product of 5 and 12: ".mulTwoNumbers(5,12); echo "<br>"; echo "The product of 5 and -10: ".mulTwoNumbers(5,-10); echo "<br>"; echo "The product of -12 and -13: ".mulTwoNumbers(-12,-13); echo "<br>"; //exit php ?> </body> </html>
When the above code is executed, it produces the following result
Multiply two number using function
The product of 5 and 12: 60 The product of 5 and -10: -50 The product of -12 and -13: 156
Product of two numbers using function – #2
Program 2
- Start PHP script
- Declare two integer variable as parameter
- Calculate product of given two numbers and return value to main
- Calling the function with argument
- Display result on the screen
- Exit PHP
In this program, first the user declare variables as parameter in function. Then when the user call the function, passing the values to variables as argument. finally , the result is calculated and display on the screen
<html> <head> <title> product of two numbers</title> </head> <body> <h3>Multiply two number using function</h3> <?php //php script //function definition with parameter function mulTwoNumbers($x, $y) { $mul=$x * $y; //calculate product of two numbers //using multiplicaion(*) operator return $mul;//return the result to main echo $mul;//print the result when calling the function } //Calling the function and print result echo "The product of 15 and 10: "; echo mulTwoNumbers(15,10); echo "<br>"; echo "The product of 25 and 10: "; echo mulTwoNumbers(25,10); echo "<br>"; echo "The product of 15 and -12: "; echo mulTwoNumbers(15,-12); echo "<br>"; //exit php ?>
When the above code is executed, it produces the following result
Multiply two number using function
The product of 15 and 10: 150 The product of 25 and 10: 250 The product of 15 and -12: -180
Similar post
Java program to multiply two numbers
C++ program to multiply two numbers
C program to multiply two numbers
Python program to multiply two numbers
Java program to multiply two floating-point numbers
C++ program to multiply two floating-point numbers
C program to multiply two floating-point numbers
Python program to multiply two floating-point numbers
PHP addition of two numbers using function