Reputation: 21667
I was wondering if someone could help me out.
I'm writing some PHP functions where I would like to use a variable inside a function and then use the updated variable outside of the function.
eg
$subTotal += $priceToShow * $ct_qty;
adminCharge($subTotal);
function adminCharge($loggedInfoprice, $subTotal){
if (($loggedInfoprice == 5)||($loggedInfoprice == 6)){
$packagingCurrency = "$";
$packagingPrice = "63";
$packagingPricing = $packagingCurrency.$packagingPrice;
}else {
$packagingCurrency = "£";
$packagingPrice = "30";
$packagingPricing = $packagingCurrency.$packagingPrice;
}
if(isset($packagingPrice)){
$subTotal = $subTotal + $packagingPrice;
}
}
<?php echo $subTotal; ?>
The echo'd $subTotal
only seems to show the value from before the function.
Is it possible to extract both $subTotal
and $packagingPricing
from the function?
Thanks in advance.
Updated the function
Upvotes: 1
Views: 2331
Reputation: 6950
you need to use pass the variable by reference or global
option 1
function adminCharge(&$subTotal){
if(isset($packagingPrice)){
$subTotal = $subTotal + $packagingPrice;
}
return array($subTotal, $packagingPrice);
}
option 2
function adminCharge()
{
global $subTotal;
if(isset($packagingPrice))
{
$subTotal = $subTotal + $packagingPrice;
}
return array($subTotal, $packagingPrice);
}
Upvotes: 0
Reputation: 29932
function adminCharge(&$subTotal){
// do things
}
or
$subTotal = adminCharge($subTotal);
function adminCharge($subTotal){
//do things
return $subTotal;
}
In the first case, you pass $subTotal
variable as a reference, so all all changes will be reflected to "outside" variable.
In the second case, you pass it and return new value, so is easy to understand why is working.
First solution - however - can lead you to side effect that will be difficult to understand and debug
If you want to "extract" (so return) two variables, you can do the following
list($subTotal,$packagingPrice) = adminCharge($subTotal);
function adminCharge($subTotal){
if(isset($packagingPrice)){
$subTotal = $subTotal + $packagingPrice;
}
return array($subTotal, $packagingPrice);
}
Upvotes: 3
Reputation: 4977
You have to understand the variable scope read here.
In short, there are 3 options.
Use global
keyword:
$a = 0;
function foo(){ global $a; $a += 1; }
Use pass by reference:
$a = 0;
function foo(&$b){ $b += 1; }
foo($a);
Collect return value of the function:
$a = 0;
function foo($b){ return $b + 1; }
$a = foo($a);
Upvotes: 0
Reputation: 1281
You don't return anything, so you could use globals but i wouldn't recommend it.. Take a look though, because that's what your asking for: http://www.php.net/global
In my opinion, change your function:
function adminCharge($subTotal){
global $packagingPrice
if(isset($packagingPrice)){
$subTotal = $subTotal + $packagingPrice;
}
return $subTotal;
}
$subTotal += $priceToShow * $ct_qty;
$subTotal = adminCharge($subTotal);
Also keep in mind you didn't had $packagingPrice in your function, so i added it with global. Otherwise it wouldn't work :)
Upvotes: 1
Reputation: 3323
function adminCharge($subTotal, $packagingPrice){
if(isset($packagingPrice)){
$subTotal = $subTotal + $packagingPrice;
}
return $subTotal;
}
Use it like this:
$subTotal = adminCharge($subTotal, $packagingPrice);
Upvotes: 1