Changing a variable function to a global function

  • Jordan
    Jordan
    Newbie 45 points
    March 10, 2015 at 5:53 pm

    Hey guys,

    I have read the codex guides and i am still a little confused on changing my variable function to have global output

    [code title=””]function getPricebs($url) {
    $decode = file_get_contents($url);
    return json_decode($decode, true);
    }

    $btcbsUSD = getPricebs(‘https://www.bitstamp.net/api/ticker/’);
    $btcbsPrice = $btcbsUSD[“last”];
    $btcbsVol = $btcbsUSD[“volume”]; // added line for volume here
    $btcbsDisplay = round($btcbsPrice, 2);
    $btcbsVol = round($btcbsVol, 2);[/code]

    This code is grabbing rates from a table. I’d like the outputs $btcbsDisplay and $btcbsVol to be global so I can access them from any page with a quick reference to the variable…. It seems reletively easy, as the guide suggest to define the variables outside the function but i am not exactly sure which ones i need to define for my case.

    Thank you!

  • M
    M
    Senior Member 278 points
    March 10, 2015 at 7:07 pm

    Assuming the above works:

    Have you tried :

    [code title=””]<?php echo $GLOBALS[‘btcbsDisplay’]; ?>[/code]

  • Jordan
    Jordan
    Newbie 45 points
    March 10, 2015 at 7:10 pm

    No i haven’t and I have a feeling that looks like it’s right, quick Q, does this load the function always as it is now global, or will this load the function only when called upon?

    Thanks so much for taking the time out of your day to help, i really appreciate it.

  • Jordan
    Jordan
    Newbie 45 points
    March 11, 2015 at 6:53 am

    M, that doesn’t seem to work. 😕 Thanks for the suggestion

Viewing 4 posts - 1 through 4 (of 4 total)

New Business Themes for WordPress 2020

Start your new website today!

View Business Themes