+1  A: 

My guess is that PHP is catching on the re-opening of the php tag <?php, try splitting just that:

$str2 = "<?" . "php print( \$myVar$myVar2 );  ?>";

Or use single quotes and concatenation:

$str2 = '<?php print( $myVar' . $myVar2 . ' );  ?>';
Cryo
Great idea! It is now working properly, although it still not answers the question "why"? ;)BTW, I also tried with the second alternative and it didn't work but the error message changed: PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Users/giove/Sites/mundial/htmltemplate.php on line 252I'll check if this solution works in all the other similar cases I have.Thanks!
nozebacle
@nozebacle I can find no reference to the way <?php is handled being changed between PHP4 and PHP5. What exactly is it that you're doing with the generated PHP code? Writing it to a file and then including it? I've run both of my lines as well as your exact original and they all echo the correct information for me even your original after being output to a file and then included (prints '2'). Either I'm not testing in the way that you're executing or it's a configuration issue.
Cryo
It's very strange indeed. The issue does not seem to be related with what I'm doing with the generated code, but it seems to be an issue with the way it is written, i.e. it's a parsing/syntactical error and not an interpretation/semantical error. As I told before, the weirdest thing is that the same code is ok in one file, but produces the error in another. I'll keep on looking at the issue and I'll try to find info about the parsing process.
nozebacle
+1  A: 

Cryo is on the right track, though I think the actual issue is that PHP evaluates variables within double-quoted strings. However, the slash should prevent the variable from being evaluated. So:

$a = "somestring"
$b = "\$a" // -> \$a
$c = '\$a' // -> \$a

I think your string is getting evaluated in an odd way such that the \ + $myVar is evaluated in a strange way.

pr1001