Last updated May 29, 2013. Created on December 19, 2008.
Edited by sunshinee, Tor Arne Thune, moshe weitzman, LeeHunter. Log in to edit this page.

Teaching PHP is beyond the scope of the Drupal documentation, but this page will introduce you to some of the most basic techniques which are important to working with themes.

Discovering your data

Both of the techniques described below use a lot of resources and can expose internal information to the public. Therefore, you should never use either technique on a production site.

Option 1: Use a PHP Function

You can use a PHP function to see all of the variables that have been passed to your template file. To do so, you can add the following code to the top of any template (.tpl.php) file in your theme.

<?php
$vars
= get_defined_vars();
print_r($vars);
?>

Option 2: Use the Theme Developer module

The Theme Developer module will you access to a live, interactive display of the variables that were used to create any part of a page, but it also includes numerous functions for outputting debugging data while you are working. Only -dev releases are available at this time.

Using the information you get

Once you have used one of the techniques above, you will be able to see numerous variables and arrays(). If you wish, you can refer directly to any of these in your template.

For example, to display the title, you would add the following code to your .tpl.php file:

<?php print $title; ?>

To display the node title along with a link to the node and some heading formatting, add the following code:

<h2 class="title">
  <a href="<?php print $node_url; ?>" title="<?php print $title; ?>"><?php print $title; ?></a>
</h2>

Arrays

The output from the print_r technique shown above will probably contain a number of arrays. For example, if you are using a taxonomy you might see something like this:

[taxonomy] => Array

An array allows a group of related data to be stored in an organized way. If you only want one item from an array to be displayed you can specify that item using its related "key".

For example, suppose the print_r technique showed you the following array:

[location] => Array
        (
            [lid] => 3
            [name] => My Place
            [street] => 235 King Edward Avenue
            [additional] =>
            [city] => Ottawa
            [province] => ON
            [postal_code] => K1N 7L8
            [country] => ca
            [latitude] => 45.431993
            [longitude] => -75.688390
            [source] => 3
            [is_primary] => 0
            [province_name] => Ontario
            [country_name] => Canada
        )

If you just wanted to display the city, you could add the following code to your .tpl.php file:

<?php print $location['city']; ?>

There are many other ways to manipulate your content using PHP. For more information, consult one of the PHP references on the web.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

PMorris’s picture

Getting variables to print from multidimensional arrays is confusing for php beginners like me. The output from get_defined_vars is a bit of a mess. For instance:

[node] => stdClass Object
                (
                    [nid] => 74
                    [type] => ddblock_news_item
                    [language] =>
                    [uid] => 1
                    [status] => 1
                    [created] => 1269380441
                    [changed] => 1269380496
                    [comment] => 0
          
<-------- EDITED some text out here to make post shorter -------->

                    [field_url] => Array
                        (
                            [0] => Array
                                (
                                    [value] =>
                                    [safe] =>
                                    [view] =>
                                )

                        )

                    [field_creds] => Array
                        (
                            [0] => Array
                                (
                                    [value] =>  PHD

                                    [format] => 2
                                    [safe] =>  PHD

                                    [view] =>  PHD

<-------- EDITED some text out here to make post shorter  -------->

I had to take into account that the variable in [field_creds] (from a cck field) was also inside a stdClass Object:

<?php print $node->field_creds['0']['value']; ?>

This will print the [value] "PHD" I still dont understand quite what $node-> does, but I hope it points someone else in the right direction as I had to spend hours searching and didn't even know what a multidimensional array was when I started...

As If’s picture

The -> sign references a function or a variable inside an object, just like [''] references a variable inside an array.

Example: imagine an object called "node" with a variable called "nid". You would write it this way: $node->nid. But if $node was an array it would have been: $node['nid']

In your comment above, "$node" is an object, "field_creds" is an array inside it, '0' is another array inside *that*, and "value" is a variable inside *that*. So following the same logic, $node->field_creds[0]['value'] means the variable called "value" inside the array of the first item (index 0) inside the field_creds array, which is inside the node object.

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

Diegen’s picture

Hey thans As if, good to understand how it works.

gekido’s picture

Instead of using print_r (and the completely unreadable output of it), a better option is to do this:

<?php
$vars
= get_defined_vars();
dprint_r($vars);
?>

Which outputs the same thing, except in a properly formatted / scrollable output that is MUCH more readable.

As If’s picture

yeah, or you could do

<?php
echo '<pre>';
print_r($vars);
echo
'</pre>';
?>

ALSO NOTE: If your data has HTML characters in it, you will need to do it this way:

<?php
echo '<pre>';
echo
htmlentities(print_r($vars,1));
echo
'</pre>';
?>

another trick is to put it inside a textarea...

<?php
echo '<textarea cols=80 rows=30>';
print_r($vars);
echo
'</textarea>';
?>

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

datarazor’s picture

Devel Is also a great tool that makes it very easy to read the variables being used somewhere, and it really shoule be listed somewhere in this guide.

With devel installed, type

<?php
$vars
= get_defined_vars();
dpm ($vars);
?>

instead of print_r to get a nice view of your variables, or call the variable directly, such as inside of node.tpl.php you could write:

<?php
dpm
($node);
?>

Merging Technology with Creativity
Fountain City Productions
http://fountain-city.com