couple of errors,
The api for streams doesn’t return offline streams so any stream offline simply won’t appear in the streams array, which also means you can end up with an empty streams array if no ones streaming.
Secondly inside the foreach loop your calling to $chdata for the name, game and url, then after that checking if $chdata is null in the wrong order but that doesn’t matter as $chdata is never going to be null apart from some weird scenario where the api has returned a completely empty stream object inside the streams array. If there are no streams online at all from the api request you simply get an empty streams array, so the foreach loop will never iterate
Finally the if statement that checks if $chdata is null the else statement uses $name which is called from $chdata[‘channel’][‘display_name’] and if $chdata is null so $name is going to be too but as above because $chdata will never be null its never going to get called in that circumstance.