Monday, November 21, 2011

The Unbelievable PHP Debug Feature: print_r()

There is a function that I personally like very much: print_r().

It prints out the raw data inside a string or array. Especially when dealing nested arrays. It is a function I cannot live without.

When dealing JSON data return from Facebook API/Graph, print_r() can be a very powerful debugging feature especially when the returning data structure is unknown.

Here's an example of examining data returned from Facebook FQL engine:



// Get your access token ($access_token) from OAuth 2.0 procedure

$fql_query_url = 'https://graph.facebook.com/' . '/fql?q=
SELECT+uid,name+FROM+user+WHERE+uid+IN+
(SELECT+uid2+FROM+friend+WHERE+uid1+=+me())' . '&' . $access_token;

// FQL query to get all the friends (UID and name) of a facebook user

$user_data = json_decode(file_get_contents($fql_query_url));

print_r($user_data);



Since the returning data structure can be very "nested" (try add in location, hometown, education, ... to the query, you'll see) and uncertain, print_r() is the way to go.

P.S.: Facebook FQL will return a empty JSON data if the return data size is more than certain bytes set by Facebook.

No comments:

Post a Comment