PHP using variables outside of a function -
i wondering if me out.
i'm writing php functions use variable inside function , use updated variable outside of 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
seems show value before function.
is possible extract both $subtotal
, $packagingpricing
function?
thanks in advance.
updated function
function admincharge(&$subtotal){ // things }
or
$subtotal = admincharge($subtotal); function admincharge($subtotal){ //do things return $subtotal; }
in first case, pass $subtotal
variable reference, all changes reflected "outside" variable.
in second case, pass , return new value, easy understand why working.
please, pay attention
first solution - - can lead side effect difficult understand , debug
update
if want "extract" (so return) 2 variables, can following
list($subtotal,$packagingprice) = admincharge($subtotal); function admincharge($subtotal){ if(isset($packagingprice)){ $subtotal = $subtotal + $packagingprice; } return array($subtotal, $packagingprice); }
Comments
Post a Comment