What you've done seems fine, but keep in mind you can't "execute" any code in the templates. It's plain html that goes there, besides the variables and the conditional system.
That's fine - the code that gets inserted there is standard Template-style code. The problem is that that code is being run, even though the template IF statement should be evaluating to false.
Is the problem that the variable in the PHP code needs to be defined or declared previously?
Because either there is a problem with that variable, or the template IF statement can't read it properly.