How to call an main variable from a function?

Discussion in 'PHP' started by kuser, Apr 21, 2013.

  1. #1
    let's say I have this:

    $coke=1; $coffe=1;
    function Bar()
    {
    echo
    "This is Bar. I have $coke cokes and $coke coffes";
    }


    Watt's the corect way to ask $coffe and coke from inside the function?
     
    kuser, Apr 21, 2013 IP
  2. kuser

    kuser Banned

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    I am not interested into an answer like : send it via function Bar($coke,$coffe)
     
    kuser, Apr 21, 2013 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Sending it is the correct way to do it. Other ways would be to use globals or store them in a superglobal (like sessions):

    
    global $coke,$coffee;
    $coke=$coffee=1;
    function Bar()
    {
        global $coke, $coffee;
        echo "This is Bar. I have $coke cokes and $coffee coffee";
    }
    Bar();
    
    Code (markup):
     
    ThePHPMaster, Apr 21, 2013 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    EricBruggema, Apr 22, 2013 IP
  5. annaharris

    annaharris Active Member

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    51
    #5
    Do you want to call both these values or just a single value ?
     
    annaharris, Apr 23, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    It would be better if you DIDN'T mention that method, given that it's a security nightmare which is why it's been disabled by default for christmas only knows how long, deprecated as of 5.3 and doesn't even EXIST in PHP 5.4+ !!!

    Hence another of those nice big red warning boxes:
    http://php.net/manual/en/security.globals.php

    Much like mysql_ functions we've been told for eight to ten years to STOP DOING THAT; to the point they're having to FINALLY shove it down people's throats.

    I would warn that in general, globals are not a reliable or secure way to be passing data around -- while sometimes they're the best answer to a problem, generally speaking you're better off just doing a Nancy Reagan on them. "Just say no!"

    Using objects to isolate scope is one of the better answers; or singletons with getters/setters so that it can't be set/rewritten by the wrong things... though that could be overboard if all you are doing is top-down execution.
     
    deathshadow, Apr 24, 2013 IP
  7. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #7
    I agree, but the TS wants it that way... so i gave him the way :D

    I still think that function ($var, $var1) is the best method! :)
     
    EricBruggema, Apr 24, 2013 IP