I hope this helps . This is more typically handled via a custom module or theme function. But assuming this is being put into a field on a node your code should work. The issue is likely a text format setting. Assuming this is Drupal 7, you can go to /admin/config/content/formats. I assume you are using PHP format. So you will want to inspect the settings for this format and make sure the PHP filter is enabled.
Drupal - How do I block (eliminate access) content in front page from anonymous user