0

I'm trying to check if the following is empty or not.

{"players":""}

I have a function that gets that from an api/site and.. well, heres the code.

function getPlayers($server) {
    // Fetches content from url and json_decodes it
    $playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
    // Attempting to check if it's empty.
    if ($playersList != "") {
        // convert list of comma-separated names into array
        $players = explode(',', $playersList->players);
        foreach ($players as $player) {
            echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
        }
    } else {
        return 'empty';
    }
}

However, using !=, empty(), or isset(), I still get an empty string, example:

https://minotar.net/avatar//32

Where it should be..

https://minotar.net/avatar/Notch/32

When it's empty, I'd like it to just return 'empty'.

I'm not sure what I'm doing wrong. Any ideas?

3
  • Have you tried if (!empty($player)) { within the foreach? Commented Dec 4, 2013 at 5:11
  • I think the function getUrl returns an object of data not array of data. Commented Dec 4, 2013 at 5:25
  • possible duplicate of How to check that an object is empty in PHP Commented Mar 4, 2015 at 22:18

5 Answers 5

1

In pure php you can check the url segments like

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);

if($segments[2] == '') { 
}
//or
if(empty($segments[2])) { 
}

//or do your condition

if you are using codeigniter you might say

if(empty($this->uri->segment(2)))

But be sure you loaded the url helper

Hope I understand your question!

Sign up to request clarification or add additional context in comments.

Comments

0

Since you were able to have some output, see my changes in the codes.

function getPlayers($server) {
    // Fetches content from url and json_decodes it
    $playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
    // Attempting to check if it's empty.
    if ($playersList != "") {
        // convert list of comma-separated names into array
        $players = explode(',', $playersList->players);
        // check conversion
        if(is_array($players) && !empty($players){
            foreach ($players as $player) {
                echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
            }
        } else {
            return 'empty';
        }
    } else {
        return 'empty';
    }
}

Comments

0

You should do this;

print_r($playersList);

just after you set it to see what it actually is. My guess is that you are not getting what you suspect from the getURL call.

Comments

0

Add one more equals sign to take type comparison into account as well

if ($playerList !== '')

Comments

0

try this

if (isset($playersList) && is_array($playersList) && !empty($playersList)) {

    // convert list of comma-separated names into array
    $players = explode(',', $playersList->players);
    foreach ($players as $player) {
        echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
    }
} else {
    return 'empty';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.