This article is for Drupal 7 developers who have tried to put PHP code in the body of a Custom Block, but can't get it to run. It assumes that you are logged in with administrative priveleges and are not yet advanced enough to be aware of the PHP filter Module ( Like me, when I first ran into this problem). It also assumes you know how to create a block and place it on your page, although you should be able to get through this without that knowledge.
To begin, create a custom Block by navigating to the Add Block page found at [your site]/admin/structure/block/add or on the Drupal 7 menu: Administration > Structure > Blocks > Add Block. After filling in the Block description and Block title, go ahead and put your PHP code in the Block body. You can mix text, HTML, and PHP code (sometimes called PHP snippets). Here is a simple example:
Today's date is:
After entering this, move down to the Text Format dropdown. This needs to be set to "PHP code". If this doesn't appear, just leave it at Filtered HTML. But realize that this is a problem we are going to have to fix. (If "PHP code" is an option on the Text Format dropdown, you should be all set and need not read any further. You have probably done what is necessary sometime in the past.)
Now, save your block, enable it by placing it on one of your theme's regions, and test it out. Most likely Drupal 7 will print out the PHP code instead of interpreting it, giving you an unexpected and ugly result.
Here is the critical fix and the point of this article:
Drupal 7 Core contains a module called "PHP filter". A fresh install of Drupal 7 has this module disabled by default. We need to enable it.
To do this, make sure you've saved your block and navigate to the Module Administration page. This is found at [your site]/admin/modules or on the Drupal 7 menu system: Administration > Modules. Scroll down to the PHP filter module and check the Enabled box. Press the Save Configuration button. At this point you should be all set, but hold on for just a second. Scroll back down to the PHP filter module again and notice that two links have been added. The first is "help". It's not long and worth reading, so check it out. The second new link is "permissions". Click on this to go to the Permissions page. Scroll down to the Filter section and notice that there is a new item called "Use the PHP code text format" and it's checkbox for the Administrator is not checked. The permission box must be checked for the role that needs to use PHP code in order for the PHP filter to show on input boxes.
(Note: User 1 automatically has all permissions including use of the PHP filter regardless whether permission is granted to any role.)
Important note: There are security considerations in enabling the PHP filter module. The PHP input filter introduces the possibility for malicious users to attack your website with malicious scripts. You should only grant permission to use the PHP filter to people you trust. Also, make sure those you grant permission to are competent PHP coders as malformed code can break your website and stop it functioning altogether. It is a good idea to create a separate role for this module (and other potentially dangerous ones) like 'developer' or 'webmaster', distinct from 'administrator' who may be a Drupal expert but not an expert coder, so you can grant it only to those who fulfill such criteria.
Anyway, back to the task at hand. Make sure you save your changes on the Permissions page, and navigate back to your Block's configuration page. (Administration > Structure > Blocks), then click on the "configure" link next to your block. Scroll down to the Text Format dropdown. You should now see a new option called "PHP code". Select it.
Remember to save your change and go test your block. It should now work as expected.
Have fun PHPing in your Blocks!