PHP Undefined Offset除非打印出来

While trying to read data from a socket ran the following code through the interpreter:

function get_song_title($stream){
    $song;
    $sc_return = array();
    $sc_return = explode("
",$stream);
    $song = $sc_return[4];
    return $song;
}

This will succeed in getting the information I need 20% of the time maybe.

However when I do:

function get_song_title($stream){
    $song;
    $sc_return = array();
    $sc_return = explode("
",$stream);
    echo $sc_return[4];
    $song = $sc_return[4];
    return $song;
}

it will succeed 100% of the time. Any method to print the array(or its elements) out will make it to work without a problem. If I simply to try return it or assign it the undefined offset 4 message comes up.

this also includes using print_r($sc_return); which will always succeed.

Any ideas why I would need to print the array element out for it to succeed all the time?

not sure about why you need echo / print out to work, but...

try to do

var_dump($sc_rturn);

to see what you are getting.

also to check for undefined index, you should do

isset($sc_return[4]);

also you can count your results using

count($sc_return);