Sumico
Sumico

Reputation: 47

PHP parse file after include

here's what I'm wanting to do:

<? include 'header.php' ?>
some html
<? include 'footer.php' ?>

in header.php...

<? //bunch of code
if (something){
//some stuff here, but no close brace
?>

in footer.php....

<? } //close the if brace ?>

Is this possible? I keep getting parse errors.

Thanks.

Upvotes: 0

Views: 385

Answers (1)

Matt
Matt

Reputation: 7040

No, you can't do that. Your brackets all need to be in the same file.

If you want to conditionally include HTML, put your logic in the file that includes header.php and footer.php

<?php include 'header.php' ?>
<?php if (/* something */): ?>
some html
<?php else : ?>
some other html
<?php endif; ?>
<?php include 'footer.php' ?>

Also, don't use ASP-style (<? ?>) short-open tags. They're being deprecated. You can still use <?= ?> to output variables and such.

Here's an interesting conversation on that topic.

And here's the notes from the PHP manual.

Upvotes: 2

Related Questions