Search code examples
phploopsfor-loopforeachnotice

How to hide error "Trying to get property of non-object"


I have the following code, which works correctly. The Twitter Friends are listed correctly, however it seems that when the last item is displayed the error "Notice: Trying to get property of non-object" is displayed 4 times.

Since the code works as it should, I would like a way to hide these errors.

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets6 = $connection->get("https://api.twitter.com/1.1/friends/list.json?screen_name=".$twitteruser."&count=".$notweets);
foreach ($tweets6 as $tweet)
{
    for($i = 0; $i < count($tweet); $i++)
    {
        echo $tweet[$i] -> name;
        echo "<br />";
    }
}

Solution

  • you can add a checker if the object has a certain property before using its value

    if (isset($tweet[$i]->name)) {
        // process
    
    }