WebToTell
PHP Function Arguments - Printable Version

+- WebToTell (https://www.webtotell.com)
+-- Forum: PHP for beginners and advance users.PHP Code samples, applications. (https://www.webtotell.com/Forum-PHP-for-beginners-and-advance-users-PHP-Code-samples-applications)
+--- Forum: PHP (https://www.webtotell.com/Forum-PHP)
+---- Forum: PHP Functions (https://www.webtotell.com/Forum-PHP-Functions)
+---- Thread: PHP Function Arguments (/Thread-PHP-Function-Arguments)



PHP Function Arguments - turgam - 03-27-2019

In order to use a function effectively, sometimes you need to pass parameters to a function in PHP. 
Here is basic example
PHP Code:
<?php
function AplusB($a$b){
   $result $a $b ; 
   return $result 
; 
} 
// usage 
echo AplusB(3,5) ; 

?>

Sometimes a parameter or one of parameters can be optional, lets say we want to use a calculate function it's in general addition but there are times we want
to use subtraction. Here is example:

PHP Code:
function AandB($a$b,$option ='addition')
 if ($option 'addition'){
   $result $a $b ; [/font][/size]
 }elseif ($option 'subtraction'){
 
 $result $a $b ; 
 
}
return 
$result;
}
 
// usage 
echo AandB(3,5) ; 
// usage 

echo AandB(5,3) ; // since $options has a default value in functions parameters, calling $option now is optional and therefore this will not raise an error                    //php. 
 // in php
//or

echo AandB(5,3,'subtraction') ; // it's valid to call AandB() function.
?>