How many times have you been working on a site and poof! something doesn't work the way you expected? For most developers this is just part of the process. There are some common ways to debug issues that are built right into ExpressionEngine. You just need to know how to use them. There are other things that aren't necessarily tools but rather features that might be missed causing errors. Let's dive in.
My Debugging Process
This is a quick run down of where my head typically goes when trying to debug something with ExpressionEngine. Some of these thoughts apply to things other than EE as well.
Dynamic="off"
This one gets everyone, from the newbie to the veteran. This is a parameter used within the weblog/channel tag that tells ExpressionEngine to ignore the URL when querying the database for results. By default EE reads your URL segments and bases the weblog/channel results off of said segments. I personally think that this should function in the reverse order and that dynamic should be set to off by default. I find myself adding that parameter to many more weblog/channel tags than not. For a good video tutorial (runtime 2:51) on dynamic="off" visit Train-EE's Dynamic = Off Explained!
Template Debugging
Is something in your template ackin a fool? Well Template Debugging might do you some good. It breaks down different pieces of the template parsing and shows you what's happening and when. It's kind enough to slap an elapsed time next to each item as well. Here's an example of what you might see:
(0.000016) - Begin Template Processing - (0.000215) URI: (0.000226) Path.php Template: site/pages (0.000241) Retrieving Template (0.000273) Retrieving Template from Database: site/pages (0.001650) Template Found (0.002578) Retrieving Template from File (0.002923) Template Type: webpage (0.003035) Parsing Site Variables etc...
Note to Developers:
You can tap into what's displayed here. If you are writing a plugin or module for ExpressionEngine you can add lines to this to help your user see what happens and when. There is a function in the Template Parser class that allows you to add a line at a time. Here's a quick sample code:
<?php
// for EE 1.6.x
global $TMPL;
$TMPL->log_item("Your message goes here and can include ".$variables);
// for EE 2.x
$this->EE =& get_instance();
$this->EE->TMPL->log_item("Your message goes here and can include ".$variables);
?>
Display SQL Queries
Say your template (design) is being parsed correctly and you don't have any obvious errors in your code but the results from some of your EE tags are not what you expected. You can turn on Display SQL Queries to see exactly what queries are being run and when. This is very helpful in certain scenarios but you will need to have a basic to moderate understanding of MySQL for it to really mean anything to you. It also helps to be familiar with the EE database tables and what is stored in them.
Note that if you are using page/template caching you may be missing some queries. The templates that are being cached will not be running queries that they would otherwise need. So for debugging queries you will either need to turn off caching for a particular template or clear your cache frequently.
Debug Status
ExpressionEngine has a useful preference available to alter who sees PHP/database error messages on your site. You can set it to either show errors to Super Admins or to anyone (not recommended). You can also turn them off all together. Keep in mind that this is specific to php.ini settings and may require some PHP configuration before using. To see if you have error reporting turned on go to Admin > Utilities > PHP Info and search for "display_errors".
That's Cool and All, but Where Are These Settings?
I'm glad you asked! These three settings (Template Debugging, Display SQL Queries, and Debug Status) are under Admin > System Preferences > Output and Debugging Preferences. As I mentioned I use them very often. Because of this they are built right in to the ER Developer Toolbar as 1-click links from your site's front-end. If you haven't checked out that ExpressionEngine extension I highly recommend that you do. It has sped up my development time and I suspect it will do the same for you!
Additional Processes
There are a few things I use often when debugging items in ExpressionEngine that are not specific to EE but are basic PHP functions. Any of the following tips can apply to your PHP scripts. I found myself using them even more when I started to learn the world of EE Add-On development.
print_r()
This is a PHP function that comes in very hand when trying to determine what is within an array or object. I learned a lot about the EE global objects in 1.6.x and the EE super object in 2.x using this function. Just run this in a PHP-enabled EE template and see what you have access to so easily:
<?php
// for EE 1.6.x
global $IN;
print_r($IN);
// for EE 2.x
$this->EE =& get_instance();
print_r($this->EE->input);
?>
I actually use this so often that I made a short TextMate snippet to automatically wrap this in <pre> tags for me to make the display vastly easier to read. If you aren't already using print_r() then it will quickly become your friend.
var_dump()
This function is actually quite similar to using print_r() except it spits our some additional information about the data. For example, the following code when using print_r() will look like each variable is identical:
<?php
$string = "1";
$integer = 1;
$array = array($string,$integer);
print_r($array);
?>
The above code should produce this:
Array
(
[0] => 1
[1] => 1
)
Those two values look pretty similar to me. But if they are, then why doesn't $string === $integer return TRUE? Take a look at it this way:
<?php
$string = "1";
$integer = 1;
$array = array($string,$integer);
var_dump($array);
?>
This time you should see this:
array(2) {
[0]=>
string(1) "1"
[1]=>
int(1)
}
This is much more beneficial because it not only shows us the values, but it shows us the type of data and the length of the data. It starts with "array(2)" which tells us that you are looking at an array with 2 items in it. Next we see the first index/value pair which is 0 => "1". It tells us that the item data type is a string, it contains 1 character and the value is "1". Next we have "int(1)" which is our second array item. It shows us that the item data type is an integer and the value is 1. If we just used print_r() we wouldn't have known any of these additional details about our data types and could possibly go crazy wondering why your conditional statement won't return what you think it should. So take var_dump() and put it in your pocket; save it for a rainy day.
That's All Folks
I hope this article helps give you a better idea of how you might debug some of your ExpressionEngine problems. These really are things I use on a daily basis and they are tremendously helpful.
Comments
Agreed, great post Erik. I haven’t tried your toolbar yet, but I might just do that now ![]()
I had to tweet it
http://twitter.com/codeignitee/status/6491247837
Thanks for the kind words Adam and Bjorn.
@adam, I’ve linked you up to my GitHub repo of a TextMate bundle of mine which include the print_r snippet I mentioned in the post. If anyone else is interested perhaps I will just make a post about it.
@bjorn, Thanks for the tweet. I’d love to hear your thoughts on the toolbar after you’ve had some time to use it
Nice writeup Erik!
One little thing thouigh: in PHP5, using references in general is deprecated (http://php.net/manual/en/language.references.php).
Your Words of Wisdom
Erik,
Some great tips here, as you know I was digging around in the SQL queries earlier to find a problem. Tips like that are invaluable for troubleshooting.
And now I’ve worked out where the problem lies, I just need to work out a way to fix it…
Thanks,
Adam
p.s. Do you mind sharing that TM snippet with us?
Adam Wiggall on Dec 8th at 3:28 pm