0

Basically, I can't use $bidPrice in my code, even though I defined it as global. Inside the If construct it's assigned a value, but outside it's still seen as undefined, for example, if I wanted to use $askPrice or $bidPrice in a function outside of the if clause.

I've also tried to define it as global inside the if clause, didn't work either.

I'm thankful for any answers :)

global $askPrice;
global $bidPrice;

    echo "\r";
    \Ratchet\Client\connect('wss://www.bitmex.com/realtime')->then(function($conn) {
        $conn->on('message', function($msg) use ($conn) {               

            if(str_replace('"askPrice":', "", strstr(strstr(strstr($msg, "[{"), '"askPrice":'), ',"askSize"', true)) != false and str_replace('"bidPrice":', "", strstr(strstr(strstr($msg, "[{"), '"bidPrice":'), ',"askPrice"', true)) != false){
                $askPrice = str_replace('"askPrice":', "", strstr(strstr(strstr($msg, "[{"), '"askPrice":'), ',"askSize"', true));
                $bidPrice = str_replace('"bidPrice":', "", strstr(strstr(strstr($msg, "[{"), '"bidPrice":'), ',"askPrice"', true));

                echo "\r";
                echo "                                 ";
                echo "\r";
                echo "bidPrice={$bidPrice} | askPrice={$askPrice}";
            }

        });

        $conn->send('{"op": "subscribe", "args": ["quote:XBTUSD"]}');
    }, function ($e) {
        echo "Could not connect: {$e->getMessage()}\n";
    });
Nico
  • 1
  • 1
    Using this way you need to define them `global` inside the function not at the top. Alternatively change the `use()` to `use ($conn, &$askPrice, &$bidPrice)` which is a much better way of handling it (IMHO). – Nigel Ren Jan 30 '19 at 20:21
  • Thanks for your answer, it really helped me! Also, sorry for posting a duplicate - I didn't find the answer on the board. – Nico Jan 30 '19 at 20:43

0 Answers0