ngplayground
ngplayground

Reputation: 21667

PHP using variables outside of a function

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

Answers (5)

alwaysLearn
alwaysLearn

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

DonCallisto
DonCallisto

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.

Please, pay attention

First solution - however - can lead you to side effect that will be difficult to understand and debug

Update

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

jancha
jancha

Reputation: 4977

You have to understand the variable scope read here.

In short, there are 3 options.

  1. Use global keyword:

    $a = 0;

    function foo(){ global $a; $a += 1; }

  2. Use pass by reference:

    $a = 0;

    function foo(&$b){ $b += 1; }

    foo($a);

  3. Collect return value of the function:

    $a = 0;

    function foo($b){ return $b + 1; }

    $a = foo($a);

Upvotes: 0

ChrisH
ChrisH

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

ty812
ty812

Reputation: 3323

function adminCharge($subTotal, $packagingPrice){
    if(isset($packagingPrice)){
        $subTotal = $subTotal + $packagingPrice;
    }

    return $subTotal;
}

Use it like this:

$subTotal = adminCharge($subTotal, $packagingPrice);

Upvotes: 1

Related Questions