Check bitstamp.net exchange rate, compare it with previous rate I have written low quality php code, I'm not a good coder ;) Help me, please, to optimise it ;)

This simple code check bitstamp.net exchange rate, compare it with previous rate and print the result.

&lt;?php
function getPrice($url) {
    $decode = @file_get_contents($url);
    return json_decode($decode, true);
}

$btc = getPrice('https://api.binance.com/api/v1/ticker/24hr?symbol=BTCUSDT');
$btcusdlast = round($btc["lastPrice"], 1);
$rbtc = ($btc["priceChange"]&gt;0) ? 'up' : ' down';

$ltc = getPrice('https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT');
$ltcusdlast = round($ltc["lastPrice"], 1);
$rltc = ($ltc["priceChange"]&gt;0) ? 'up' : ' down';

?&gt;

    &lt;h2&gt;BTC 
    $ &lt;?=$btcusdlast, ' ', $rbtc;?&gt;&lt;/h2&gt;

    &lt;h2&gt;LTC 
    $ &lt;?=$ltcusdlast, ' ', $rltc;?&gt;&lt;/h2&gt;


But it's low quality and work very slowly.
Id make your code more functional (if your not going to use OOP) the fact that this code is slow is probably your internet connection (as mentioned in the comments) 

First as the api is returning common keys I would just make the rounding &amp; ternary operator as one function you can pass a response to,

Also because you aren't using full names and just short codes the whole code works in a loop, this way you can just add more coin short codes into the $coinsToFetch array 

&lt;?php
function fetchAndDecodeData($url)
{
  return json_decode(@file_get_contents($url), true);
}

function getValuesFromBinanceData($data)
{
  return [
    "priceChange"=&gt;$data["priceChange"] &gt; 0 ? "up" : "down",
    "lastPrice"=&gt;round($data["lastPrice"], 1)
  ];
}

$coinsToFetch = ["BTC", "LTC"];

foreach ($coinsToFetch as $coin) {
  $coinData = fetchAndDecodeData('https://api.binance.com/api/v1/ticker/24hr?symbol=' . $coin . 'USDT');
  $data = getValuesFromBinanceData($coinData);
  echo "&lt;h2&gt; $coin " . $data['lastPrice'] . $data['priceChange'] . "&lt;/h2&gt;";
}


To address the issue of the speed of your script in relation to your internet you could cache the results (redis) or store them in database (mysql) this would allow you to do some sort of code like this (psudeo code)

If its less than 5 minutes sinse the cache
  Load the data from the cache
Else 
  Load from binance api


If you are going to use the caching / storage I would convert this into two scripts;


That runs as cron job to populate cache / storage every x number of minutes
To display the data