Facebook Cookbook - Jay Goldman [50]
Often something like this can get by and turn into a massive loop:
for ($x=0;$x<100000;++$x)
{
doSomethingWith($array[$x]["slice"]);
}
In this example, you’re iterating over and over again on that array, and with every loop, PHP is taking the string “slice” and checking it to see whether it has any variables in it for reassignment. This wastes memory and cycles on a much larger scale than a single assignment or if statement.
When upgrading older code that uses a lot of double quotes to a single-quote model, the biggest gotcha can come from special escaped characters. These characters include \n, \t, and \r, which, when used in a double-quoted string, get replaced with a newline, tab, and carriage return, respectively. You can use the concatenation method for keeping these structures intact, though, and still save processor time and memory. For example:
echo 'This is a line with a newline at the end' . "\n";
Proper quoting of strings can sometimes be a pain, but the rewards over time in terms of speed, hardware costs, man hours, and your sanity can be extraordinary in any app that you want to scale to a global audience. And who doesn’t want that?
Demo Applications
Problem
It would be so awesome if I had some demo applications I could download and play with!
Solution and Discussion
Lucky for you, there are a bunch of demo apps that you can grab and dig through. You’ll find them on the Wiki at http://wiki.developers.facebook.com/index.php/Demos. The current crop of three demos includes:
Footprints
An extremely basic application included with the PHP 4 and PHP 5 clients. You can play with a live demo at http://apps.facebook.com/footprints/.
Restaurants
A more advanced demo showing some of the more complex features, including Mock Ajax, FBJS, and the Data Store API. It’s backed by premade data on your MySQL server and/or the Facebook Data Store API.
Who’s Showing Up
Another “real” application, demonstrating building applications that are installable both for users and for Facebook Pages, such as those for businesses, bands, and brands.
In addition to the apps provided by Facebook, Jesse Stay (author of O’Reilly’s excellent FBML Essentials and coauthor of I’m on Facebook—Now What???) has built an app that demonstrates almost all of the FBML tags; see http://apps.facebook.com/fbmlessentials.
Developer Documentation
Problem
This book is the greatest thing since sliced bread, but I need more technical detail on a specific topic and/or long listings of sample code.
Solution
Facebook Platform launched with some official documentation (see http://developers.facebook.com, not to be confused with http://www.facebook.com/developers, which is the Developer app), but that quickly became out-of-date. Realizing that there was a veritable army of people who were happy to help keep the docs relevant, the Facebook team quickly launched a wiki and stopped updating the old docs, which languish as a reminder of days gone by. If you’re looking for info or want to contribute to keeping it real, check out the Developers Wiki at http://wiki.developers.facebook.com.
Discussion
The Wiki is a real treasure trove of information but can be very difficult to navigate. Since it’s essentially crowdsourced documentation, different authors have come up with very different ways to organize different sections, which leads to a palpable sense of disorientation (as an example, check out the excellent index page for the FBML section and the chaotic jumble of FBJS’s single page). The joy of wikis is that we’re all free to update and edit them, and so I encourage you to contribute back to the community by jumping in and helping to keep it orderly, up-to-date, and well-organized.
Test Consoles
Problem
It can be a real pain to set up a testing framework as a Facebook app if you just want to try out a few lines of API code. Surely there must be a better way!
Solution
Facebook makes three test consoles available to developers:
API Test Console
Found at http://developers.facebook.com/tools.php?api,