passing arguments by value

Passing Arguments by Value is used to often find it useful to pass data into a function.

passing arguments by value Syntax :

<?php
function_name("value1","value2");
?>

Create a function that calculates an item's total cost by determining its sales tax and then adding that amount to the price.

passing arguments by value Example:


<?php
function calcSalesTax($price, $tax)
{
  $total = $price + ($price * $tax);
  echo "Total cost: $total";
}
?>

This function accepts two parameters, aptly named $price and $tax, which are used in the calculation. Although these parameters are intended to be floating points, because of PHP's weak typing, nothing prevents you from passing in variables of any datatype, but the outcome might not be what you expect. In addition, you're allowed to define as few or as many parameters as you deem necessary; there are no languageimposed constraints in this regard.Once defined, you can then invoke the function as demonstrated in the previous section. For example, the calcSalesTax() function would be called like so.

calcSalesTax(15.00, .075);

Of course, you're not bound to passing static values into the function. You can also pass variables like this:

<?php
$pricetag = 15.00;
$salestax = .075;
calcSalesTax($pricetag, $salestax);
?>

When you pass an argument in this manner, it's called passing by value. This means that any changes made to those values within the scope of the function are ignored outside of the function.





Content