1

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

5 Answers 5

3
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);
}
Sign up to request clarification or add additional context in comments.

7 Comments

This (pass by reference) actually is discouraged by PHP and deprecated as of PHP 5.3.0
I have updated my question to ask how to extract more than one variable from the function.
Shouldn't reference the variable, it's not an object?!
@MartinHohenberg I thought only call-time pass-by-reference was deprecated, but actually passing by reference in the function definition is perfectly okay?
@DonCallisto it's perfectly fine to pass by reference when defining the function, just not when calling it.
|
1
function adminCharge($subTotal, $packagingPrice){
    if(isset($packagingPrice)){
        $subTotal = $subTotal + $packagingPrice;
    }

    return $subTotal;
}

Use it like this:

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

4 Comments

You're missing $packagingPrice, it should be globalised since it's not a parameter..
@ChrisH Fixed that ... Globalizing stuff is a bad idea though, it makes debugging more complicated. Best practice is to never do that.
It sure is! However, it's his function and the var isn't set, so it must be used elsewhere. It should be the second parameter, but since it's not it doesn't exist in the function, globalise the var and it does exist..
For more information why globalizing variables is a horrible idea read c2.com/cgi/wiki?GlobalVariablesAreBad
1

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 :)

Comments

0

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);

Comments

0

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);

 }

2 Comments

from where does $packagingPrice comes from .. ?
If you want to xtract both the values you have to return them as array .. see my updated answer for return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.