Ok, the solution was fragment caching. First you have to set your secret phrase by setting
define('W3TC_DYNAMIC_SECURITY', 'my_string');
in wp-config.php
and then you can use PHP code like this
<!-- mfunc echo "Hello World<br/>"; --><!-- /mfunc -->
which outputs this dynamic code
<?php echo "Hello World<br/>"; ?>