Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP Function Arguments
#1

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.
?>
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)