0

When using PHP code and HTML throughout the whole page of a website, is it quicker to state a condition than define the whole page, or keep going back to that condition at different parts of the page? For example:

<?php if(isset($_SESSION['token'])) { ?>
<html> Lots of HTML </html>
<?php } else if (!isset($_SESSION['token'])) { ?>
<html> Lots of HTML </html> <?php } ?>

So here I'm just basically defining the whole page twice with different stuff depending on whether the user is logged in or not.

Or I could do..

<?php if(isset($_SESSION['token'])) $loggedIn = true; 
else if (!isset($_SESSION['token'])) $LoggedIn = false; ?>

<html>
Some HTML stuff ...
<?php if($LoggedIn == true) echo "<div>You are now logged in</div>";
else if($LoggedIn == false) echo "<div>Please log in to continue</div>"; ?>

Some more HTML ...
<?php if($LoggedIn == true) // Etc. ?>

</html>

And here I'm just checking back to whether the user is logged in or not within the same HTML tags and displaying the appropriate content.

Also, could anyone tell me whether its better to echo HTML, or just cut off the PHP, use normal HTML and then reopen PHP and close the bracket.

I tried to be as clear as possible but was finding it hard.

1
  • I might get flamed for this, but I'd say echoing small amounts of HTML that's got a lot of PHP-generated text in it (calculated numbers, etc) is far better than continually opening and closing PHP tags all over the place. Of course, if you have more HTML than PHP then the opposite is preferable, although Heredoc must exist for a reason... Commented Jan 7, 2012 at 2:21

3 Answers 3

3

Use the second variant; repeating stuff is bad. But note that you could write far more succinctly:

<?php $loggedIn = isset($_SESSION['token']); ?>

<html>
..some html stuff..
<?php if($LoggedIn) echo "<div>You are now logged in</div>";
else echo "<div>Please log in to continue</div>"; ?>

..some more html...
<?php if($LoggedIn) // etc....................... ?>

</html>

But note that in general, mixing PHP and HTML inline like this becomes difficult to maintain. You should consider separating the two using a template engine.

Sign up to request clarification or add additional context in comments.

Comments

1

Generally speaking I am trying to leave HTML intact, as is, and just inject PHP code wherever I need. I don't fancy echo'ing out HTML if I can avoid it. I pretty much just leave small snippets of HTML in echo sections, like adding and around text.

I'd avoid using your first example (the complete page "duplicated"). Avoid duplicating HTML as much as possible, and just cover the changes inside PHP logic.

Comments

0

Cutting off the PHP is better practice and more common. Plus, whatever software you code in will recognize it as html, which it won't if it was a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.