PHP: Is SimpleXML Loaded?
Douglas Karr at 9:53 pmThanks for stopping by my personal blog on Marketing Technology! Over 50,000 visitors a month find my content worth returning for, so don't forget to subscribe to the Marketing Technology Blog RSS feed or to the Marketing Technology Email to have new content sent directly to your inbox. You may also find my other business blog helpful, Social Media Domination.
There are a couple plugins that I’ve built, Technorati Rank and Jaiku Activity, that require PHP5+ since they utilize SimpleXML.
SimpleXML is a much easier and better performing method of parsing XML responses from APIs. The problem, though, is that I would get at a few emails a day or week asking me why the user couldn’t load the program and it resulted in errors.
Apparently, my notices on the plugins and on the project pages weren’t enough, so I did the right thing and added functionality to both plugins to verify the SimpleXML extension is loaded.
PHP Function to check the SimpleXML extension is loaded:
function isSimpleXMLLoaded() {
$array = array();
$array = get_loaded_extensions();
$result = false;
foreach ($array as $i => $value) {
if (strtolower($value) == “simplexml”) { $result = true; }
}
return $result;
}
Now, within the functions that use SimpleXML, I can simply ensure it’s loaded before I actually try the SimpleXML call. If
if (!isSimpleXMLLoaded()) {
echo “Host your site somewhere else!”;
return;
}
I know I’ve got some PHP gurus that keep an eye on my blog, let me know how I did! I’ve released minor updates to both Plugins to utilize this method.

I did notice a one bug which probably doesn’t raise an error.
if ($value = “SimpleXML”) { $result = true; }
should be
if ($value == “SimpleXML”) { $result = true; }
Although for safety sake. I prefer.
if (strtolower($value) == “simplexml”) { $result = true; }
You could also use ‘extension_loaded’ which takes the extension name to check (case sensitive).
$loaded = extension_loaded(”SimpleXML”);
Returns TRUE or FALSE.
P.S. Don’t drink coffee myself but I may put a ‘buy me a box of donuts’ button
I’ve modified the code and the blog post. Question: Any advantage of one over the other? I guess the extension_loaded is a much cleaner and quicker way of dealing with this!
Thanks Nick!