close

Why should you register with JoomlaDigger.com?

JoomlaDigger.com is a free community website where users can learn about web development, Joomla! and how to bring more traffic to their websites while contributing their own knowledge. Our members enjoy several benefits, including email notifications of new products and updates, access to the forums, and the ability to give back to the community by submitting content to be posted on JoomlaDigger.com (with links directly to their own websites). And best of all, it's FREE!
Membership Features
  • Email notifications for new content, extensions and updates.
  • Full access to our forums for Joomla! development and extension support.
  • Ability to contribute content and weblinks to gain prominent links to your own website.
Top Panel
Why Register?
Top Panel

PHP Coding Tip: The print_r() Function

(23 votes)


Digg!

Reddit!

Del.icio.us!

JoomlaVote!

Technorati!

Submit to Open Source Software!

StumbleUpon!

DZone!

Here's a quick but extremely useful tip for PHP developers. When working on plugins or extensions for an existing framework, use the print_r() function on the API objects to find out what data structures they contain. Great PHP help tip.

This function is a big help for Joomla! development. There really isn't much documentation for Joomla! 1.5. The basic framework is there, but examples are extremely basic and don't have the information I would expect from a top-notch open source product (which Joomla! certainly is). This means that a lot of your coding will be a trial and error process.

You can speed up this process by using PHP's print_r() function. Print_r() basically takes a variable, even something complex like an array of arrays, and prints it to the screen in a humanly redable format, helping you figure out what data the object contains and how you can use it.

I found it very helpful when creating the Joomla! 1.5 version of the Ultimate Social Bookmarking Plugin. This was my first Joomla! 1.5 extension and I needed to know what data the $article variable contained. I wrapped it with a set of <pre></pre> tags to automatically format the data. Example:

echo '<pre>';
print_r($article);
echo '</pre>';

This simple chunk of PHP debugging code printed all of the information I needed to the screen. It told me exactly which fields existed within the $article object and what values they contained. I also learned that $article->alias only exists when the full content item is displayed, not on the homepage or blog/section/category listing pages. Here's an example output of the above code (with actual data values removed):

stdClass Object
(
[id] => 0
[title] => title
[alias] => alias
[title_alias] => 
[introtext] => intro text
[state] => 0
[sectionid] => 0
[mask] => 0
[catid] => 0
[created] => date
[created_by] => 0
[created_by_alias] => 
[modified] => date
[modified_by] => 0
[checked_out] => 0
[checked_out_time] => date
[publish_up] => date
[publish_down] => Never
[images] => 
[urls] => 
[attribs] => atts
[version] => 0
[parentid] => 0
[ordering] => 0
[metakey] => 
[metadesc] => 
[access] => 0
[hits] => 0
[metadata] => 
[author] => author
[usertype] => permissions
[category] => cat
[section] => sec
[slug] => slug
[catslug] => catslug
[groups] => groups
[sec_pub] => 0
[cat_pub] => 0
[sec_access] => 0
[cat_access] => 0
[rating_count] => 0
[rating] => 0
[text] => main article text
[parameters] => JParameter Object
(
[_raw] => show_noauth=0
show_title=1
link_titles=0
show_intro=1
show_section=0
link_section=0
show_category=0
link_category=0
show_author=0
show_create_date=1
show_modify_date=0
show_item_navigation=0
show_readmore=1
show_vote=0
show_icons=1
show_pdf_icon=0
show_print_icon=0
show_email_icon=0
show_hits=0
feed_summary=0
[_xml] => 
[_elements] => Array
(
)
[_elementPath] => Array
(
[0] => /path
)
[_defaultNameSpace] => _default
[_registry] => Array
(
[_default] => Array
(
[data] => stdClass Object
(
[show_noauth] => 0
[show_title] => 0
[link_titles] => 0
[show_intro] => 10
[show_section] => 0
[link_section] => 0
[show_category] => 0
[link_category] => 0
[show_author] => 0
[show_create_date] => 0
[show_modify_date] => 0
[show_item_navigation] => 0
[show_readmore] => 0
[show_vote] => 0
[show_icons] => 0
[show_pdf_icon] => 0
[show_print_icon] => 0
[show_email_icon] => 0
[show_hits] => 0
[feed_summary] => 0
[show_description] => 0
[show_description_image] => 0
[num_leading_articles] => 0
[num_intro_articles] => 0
[num_columns] => 0
[num_links] => 0
[show_pagination] => 0
[show_pagination_results] => 0
[show_feed_link] => 0
[html_title] => title
[robots] => index, follow
[show_page_title] => 0
[menu_image] => 0
[secure] => 0
[page_title] => title
[page_description] => description
[popup] => 0
)
)
)
[_errors] => Array
(
)
)
[readmore_link] => /link.html
)

Article by P.J. Swesey
Open source software news and directory of open source software.
Login and submit your own articles!
This full listing showed me everything available in the $article object. It gave me a much better understanding of how the backend of Joomla! 1.5 handles articles and what data was actually available to me. This information really couldn't have been found in any documentation, and it takes too long to get a response from an experienced developer at the Joomla! forums.

Overall, PHP's print_r() function is awesome for debugging and development when you are working with objects that you just don't know enough about. It saves a lot of time and frustration when trying to code in a new environment. Definitely a helpful function.

 
RSS Feed Add to iGoogle Add to Yahoo Add to PageFlakes Add to Netvibes Add to Technorati

Copyright © 2007-2008 JoomlaDigger.com. All rights reserved. Website by P.J. Swesey