PHP Parameterized functions are the functions with parameters. You can pass any number of parameters inside a function. These passed parameters act as variables inside your function.
The output depends upon the dynamic values passed as the parameters into the function.
Eg: Addition and Subtraction
we have passed two parameters $x and $y inside two functions add() and sub().
<!DOCTYPE html>
<html>
<head>
<title>Parameter Addition and Subtraction Example</title>
</head>
<body>
<?php
//Adding two numbers
function add($x, $y) {
$sum = $x + $y;
echo "Sum of two numbers is = $sum <br><br>";
}
add(50, 60);
//Subtracting two numbers
function sub($x, $y) {
$diff = $x - $y;
echo "Difference between two numbers is = $diff";
}
sub(100, 50);
?>
</body>
</html>
Output :
Sum of two numbers is = 110
Difference between two numbers is =50
Eg: Addition and Subtraction with Dynamic number
we have passed two parameters $x and $y inside two functions add() and sub().
<?php
//add() function with two parameter
function add($x,$y)
{
$sum=$x+$y;
echo "Sum Of Number = $sum <br><br>";
}
//sub() function with two parameter
function sub($x,$y)
{
$sub=$x-$y;
echo "Difference = $sub <br><br>";
}
//call function, get two argument through input box and click on add or sub button
if(isset($_POST["add"]))
{
//call add() function
add($_POST["first"],$_POST["second"]);
}
if(isset($_POST["sub"]))
{
//call add() function
sub($_POST["first"],$_POST["second"]);
}
?>
<form method="post">
Enter first number: <input type="number" name="first"/><br><br>
Enter second number: <input type="number" name="second"/><br><br>
<input type="submit" name="add" value="ADDITION"/>
<input type="submit" name="sub" value="SUBTRACTION"/>
</form>
Suppose We passed the following number
First Number =50
Second Number =30
Now clicking on the ADDITION button, we get the following output.
Output:
Sum Of Number=80
Suppose We passed the following number
First Number =299
Second Number =99
Now clicking on the SUBTRACTION button, we get the following output.
Output:
Difference =80
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning