PHP in Templates

ExpressionEngine allows you to place PHP code within your Templates so that it can be executed. This allows you to do all sorts of things dynamically with your content. If you've come from pMachine Free or Pro this is a big change and should add a great deal of flexibility to what you're accustomed to being able to do.

PHP preferences can be set for each Template individually, which means that you can decide which of your Templates, if any, will be able to parse PHP code inside it. You can access the settings for this under the Prefs link for any Template in the Templates section.

PHP settings for each Template are honored on a per-Template basis, which means that you can embed a Template that has PHP enabled into another Template which does not have PHP enabled.

Template Preferences: PHP screen

Allow PHP in this Template?

Setting this preference to "Yes" will allow you to have PHP code parsed within your Template.

Be aware that there could be security consequences to allowing PHP code to be parsed. If you allow PHP code to be parsed, then it would be possible for you to include code which might do something such as alter your database content. Make sure that anyone who has access to a Template with PHP enabled knows what they are doing.

Stage at which PHP should be parsed

There are two options for when PHP gets parsed:

  1. Input: PHP parsed at the "input" stage will be parsed before ExpressionEngine Tags get parsed. This setting allows you to affect the Template before it is interpreted. That will allow you to do things such as:

    {exp:weblog:entries limit="<?php echo $limit; ?>"}

    or

    <?php
    if ($size == "big")
    {
    echo "{exp:weblog:entries limit='50'}";
    }
    else
    {
    echo "{exp:weblog:entries limit='2'}";
    }
    ?>
  2. Output: PHP parsed at the "output" stage will be the last thing done, which means it will be parsed after the ExpressionEngine tags are interpreted. This will allow you to use PHP to affect the "rendered Template".