I'm pretty sure you need to escape some of the symbols, I could be wrong though.
Try
PHP Code:
eval('$variable= "' . fetch_template('template_name') . '";');
inplace of the second line. And stick the <if> ...</if>inside a template (template_name). This would just check if my theory is correct, but of course you could use this too.
You could also (which would make more sense to me) have all of it inside the template you are using (not php one) as nested <if>s instead of using the php one, or do the opposite: all in php.
IMO anyway.