your image

PHP Parameterized Function - javatpoint

javapoint
Related Topic
:- PHP information technology

 

Next →← Prev

PHP Parameterized Function

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.

They are specified inside the parentheses, after the function name.

The output depends upon the dynamic values passed as the parameters into the function.

PHP Parameterized Example 1

Addition and Subtraction

 

In this example, we have passed two parameters $x and $y inside two functions add() and sub().

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Parameter Addition and Subtraction Example</title>  
  5. </head>  
  6. <body>  
  7. <?php  
  8.         //Adding two numbers  
  9.          function add($x, $y) {  
  10.             $sum = $x + $y;  
  11.             echo "Sum of two numbers is = $sum <br><br>";  
  12.          }   
  13.          add(467, 943);  
  14.   
  15.          //Subtracting two numbers  
  16.          function sub($x, $y) {  
  17.             $diff = $x - $y;  
  18.             echo "Difference between two numbers is = $diff";  
  19.          }   
  20.          sub(943, 467);  
  21.       ?>  
  22. </body>  
  23. </html>  

Output:

PHP Parameterized Example 2

Addition and Subtraction with Dynamic number

In this example, we have passed two parameters $x and $y inside two functions add() and sub().

  1. <?php  
  2. //add() function with two parameter  
  3. function add($x,$y)    
  4. {  
  5. $sum=$x+$y;  
  6. echo "Sum = $sum <br><br>";  
  7. }  
  8. //sub() function with two parameter  
  9. function sub($x,$y)    
  10. {  
  11. $sub=$x-$y;  
  12. echo "Diff = $sub <br><br>";  
  13. }  
  14. //call function, get  two argument through input box and click on add or sub button  
  15. if(isset($_POST['add']))  
  16. {  
  17. //call add() function  
  18.  add($_POST['first'],$_POST['second']);  
  19. }     
  20. if(isset($_POST['sub']))  
  21. {  
  22. //call add() function  
  23. sub($_POST['first'],$_POST['second']);  
  24. }  
  25. ?>  
  26. <form method="post">  
  27. Enter first number: <input type="number" name="first"/><br><br>  
  28. Enter second number: <input type="number" name="second"/><br><br>  
  29. <input type="submit" name="add" value="ADDITION"/>  
  30. <input type="submit" name="sub" value="SUBTRACTION"/>  
  31. </form>     

Output:

We passed the following number,

Now clicking on ADDITION button, we get the following output.

Now clicking on SUBTRACTION button, we get the following output.

 

Comments