Saturday, September 2, 2017

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.

PHP Function With One Argument :
In this example we will see, how we can pass single parameter to function.
<?php  
         // single argument function
         function add($x) {  
            $sum = $x + $x;  
            echo "Addtion of two numbers is $sum <br>";  
         } 
         
         // single argument function
         function multiply($x) {  
            $sum = $x * $x;  
            echo "multiplication of two numbers is $sum <br>";  
         }  

 add(10);
multiply(10);
         
?>
Output :
---------------------------

PHP Function With Two Arguments : 
In this example we will see, how we can pass two arguments/parameters to function.
<?php  
         // two argument function
         function add($x,$y) {  
            $sum = $x + $y;  
            echo "Addtion of two numbers is $sum <br>";  
         } 
         
         // two argument function
         function multiply($x,$y) {  
            $sum = $x * $y;  
            echo "multiplication of two numbers is $sum <br>";  
         }  

add(10,30);
multiply(10,20);
         
?>
Output:
--------------------
Addtion of two numbers is 40
multiplication of two numbers is 200

NOTE:
Similarly, we can use N numbers of parameters depending upon the requirement and Programming logic.


No comments:

Post a Comment