QUESTION:

Why doesn’t $thisarticle work in my PHP code?
<?php ... ?> doesn’t work anymore!



ANSWER:

You can use PHP code in a Textpattern page template or form like this:

<txp:php>echo 'Hello world'; </txp:php>

Make sure you use <txp:php></txp:php> instead of <?php ... ?> or <? ... ?>.

There is an Advanced Preference that can be used to disable PHP code.

PHP code will also work inside an article body if Textpattern is configured to allow it. If you are using Textile, be sure to escape it:

notextile. <txp:php> // your PHP code </txp:php>

We strongly recommend you set your Production Status to Debugging when adding or modifying PHP code in Textpattern.

You can include() an external script, provided you understand how variable scope and include paths work. Your PHP code will not be executed in the global scope, so you’ll have to explicitly use the global keyword.

When including or opening a file from PHP, you’ll need
To see the current working directory and include path:

<txp:php>var_dump(getcwd(), get_include_path());</txp:php>

If your file resides in Textpattern’s admin folder, you can use the defined constant txpath to refer to that path – i.e. include(txpath.'/myfile.php');.

Please be aware that any PHP code you run from Textpattern, whether included directly in a page or in a separate file, will share database connections, function, variable and class names and so on with Textpattern. PHP doesn’t support separate namespaces, so it’s up to you to ensure there are no clashes.

Some caveats to be aware of when using <txp:php>:

Variable Scope

Your PHP code will not be executed in the global scope, so you’ll need to explicitly declare global variables as required. If you want to pass data between separate <txp:php> blocks, you’ll need to do something like this:

<txp:php>
global $foo;

$foo = 'bar';
</txp:php>
<txp:php>
global $foo;

echo $foo;
</txp:php>

Escaping to XHTML

While you can use multiple PHP blocks on a single page, each <txp:php>...</txp:php> block must be a valid block of code by itself. This means you can’t escape from PHP to HTML and back like you can with <?php ... ?>. This will not work:

<txp:php>if ($something) {</txp:php>
foo
<txp:php>}</txp:php>

Use something like this instead:

<txp:php>
if ($something) {
echo 'foo';
}
</txp:php>

Debugging

Other than the caveats listed above, Textpattern doesn’t interfere with your PHP code. If your code isn’t working as expected, you’ll need to use standard debugging techniques to find out what’s wrong.

If you’ve followed the recommendations above and set Production Status to Debug, Textpattern will set PHP’s error reporting settings to display all errors, warnings and notices. This should help you find errors like misnamed variables.

For other problems, the most effective debugging technique is also the simplest: good old fashioned debug printing. Use PHP functions like print(), var_dump() and var_export() to display important data at key points in your code.

Textpattern provides a handy function called dmp() that will display a message or array surrounded by <pre>...</pre> tags for easy viewing. For example:

<txp:php>
dmp('My php code');

global $thisarticle;

dmp($thisarticle);
</txp:php>

dmp() uses the print_r() function to display arrays, and print() for everything else.

PHP debugging techniques are described in more detail in this article.