Small Bit Of PHP Code Seems To Make No Sense

Sitepoint Members,

I found this code

<?php
}
?>

twice in a common piece of software. Isn’t it true that either one piece of PHP code is nested inside another or one piece of code is separate from another piece of php code; you can’t have overlapping php commands can you? In other words, in the code above, it can’t be that <?php is for one php command and ?> is for a different php command, is it?

Thanks,

Linda

Hi Linda,

What you’ll probably find is there is a loop a little further up the code, and that the code breaks out of php to display the html, then back into php again to exit the loop.

Something akin to:-


<?php
$result = $database->query('SELECT id, name FROM table');
while(false !== ($record = $result->fetch())){
?>

<div>
    <h4>
        <strong><?php echo $record['id']; ?></strong><?php echo $record['name']; ?>
    </h4>
</div>

<?php
}
?>

Anthony,
No kidding. Is that a unique to php?

Linda

Anthony (and visitors),
On another site someone wrote me, “What its doing is closing an opening bracket that appeared earlier in another PHP code block. This may look unusual, but it works.” and that Wordpress uses it all the time.

Thanks for the help,

Linda

Yes, that is correct. It is not necessarily a loop, it could also be an if statement, or a function or class.

<?php [some-block] { ?>

<?php } ?>

is common in php.

You see this in asp.net and i think jsp as well, depending what templating engines you use.

Thanks Vodoo and Anthony,

Linda