11

I am fairly new to PHP. I have a function which checks the cost of price. I want to return the variable from this function to be used globally:

<?
function getDeliveryPrice($qew){
    if ($qew=="1"){
        $deliveryPrice="60";
    } else {
        $deliveryPrice="20";
    }
    return $deliveryPrice;                          
}
// Assuming these two next lines are on external pages..
getDeliveryPrice(12);
echo $deliveryPrice; // It should return 20

?>
8
  • @M1th I wish there was some downvote on comments for fanboy mantra. Commented Sep 6, 2012 at 9:11
  • @TheBlackBenzKid: There is: flagging comments for moderator attention. Already did so, will be gone in a little while. Commented Sep 6, 2012 at 9:12
  • 1
    @TheBlackBenzKid Just flag it. Commented Sep 6, 2012 at 9:12
  • Thanks. I have flagged. Surely I asked in the correct manor so why the OMG so newb attitude. Commented Sep 6, 2012 at 9:13
  • 1
    @TheBlackBenzKid Remember, this is the internet. For every polite, helpful and friendly person there are hundreds of the opposite. SO is pretty good at weeding out the folks we don't want but it is sort of whack-a-mole :) Commented Sep 6, 2012 at 9:15

4 Answers 4

15

You should simply store the return value in a variable:

$deliveryPrice = getDeliveryPrice(12);
echo $deliveryPrice; // will print 20

The $deliveryPrice variable above is a different variable than the $deliveryPrice inside the function. The latter is not visible outside the function because of variable scope.

Sign up to request clarification or add additional context in comments.

4 Comments

Also worth mentioning that in the comparison it is being checked against a string value perhaps?
@Fluffeh: Eh... no practical difference. I wouldn't mention that to a beginner.
Is there somehow I can say returnAsGlobal $deliveryPrice;
@TheBlackBenzKid: There is, but that's one of the worst practices available to you and it's only "global or nothing" -- there is no option to say "make this available to the caller" if the calling code is not in the global scope. Take my advice and do not go there. There is also the possibility of the function accepting an "out parameter" by reference but again: this is not how that feature should be used.
3
<?
function getDeliveryPrice($qew){
    if ($qew=="1"){
        $deliveryPrice="60";
    } else {
        $deliveryPrice="20";
    }
    return $deliveryPrice;                          
}

$price = getDeliveryPrice(12);
echo $price;

?>

1 Comment

Please add some explanation to your answer such that others can learn from it
3
<?php
function getDeliveryPrice($qew){
   global $deliveryPrice;
    if ($qew=="1"){
        $deliveryPrice="60";
    } else {
        $deliveryPrice="20";
    }
    //return $deliveryPrice;                          
}
// Assuming these two next lines are on external pages..
getDeliveryPrice(12);
echo $deliveryPrice; // It should return 20

?>

2 Comments

sheri prajul :)
Please add some explanation to your answer such that others can learn from it
2

As some alrady said, try using classes for this.

class myClass
{
    private $delivery_price;

    public function setDeliveryPrice($qew = 0)
    {
        if ($qew == "1") {
            $this->delivery_price = "60";
        } else {
            $this->delivery_price = "20";
        }
    }

    public function getDeliveryPrice()
    {
        return $this->delivery_price;
    }
}

Now, to use it, just initialize the class and do what you need:

$myClass = new myClass();
$myClass->setDeliveryPrice(1);

echo $myClass->getDeliveryPrice();

1 Comment

Please add some explanation to your answer such that others can learn from it - what's the point in using a class here? Which problem does it solve? The variable itself isn't made global through this

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.