I’m modifying a wordpress template and I’m curious as to how this is a valid control structure in PHP. Anyone have any insight?
<?php if(condition): ?>
<?php if(!condition || !function()) ?>
<?php elseif(condition): ?>
<?php if(!condition || !function()) ?>
<?php endif; ?>
If I remove all the tags, I get (with more sane indentation):
<?php
if(condition):
if(!condition || !function())
elseif(condition):
if(!condition || !function())
endif;
?>
which is invalid because the indented if statements don’t end. So how/why is this code valid if there are opening and closing php tags everywhere?
Edit for Kerrek SB. Make a php file and run it. It’s valid:
<?php if(true): ?>
<?php if(true) ?>
<?php endif; ?>
<?php echo 'here'; ?>
Your (reduced) example code is equivalent to this:
Or even:
By closing off the
<?phptag, you appear to get an “empty statement” for free.Your real example could be one-lined like so:
But note that an
elseifmakes this ambigous!We’d have to disambiguate this:
Now it’s clear, but now we could have spared ourselves the colon syntax altogether.
Thanks to Lekensteyn for pointing this out!
See the discussion below for further oddities.