1

This probably might be a silly question, but what I am trying to do is to make an if statement to do the following:

<?php if ($_SESSION['login'] == true) { ?>
Display this HTML code (without converting it to PHP echos
<?php } else { ?>
Display this instead
<?php } ?>

Or will I need to echo, and in turn escape all the required characters in order to do what I am after.

Thanks

3
  • 4
    If the question is "Will this work?" … try it and see. Commented Nov 3, 2010 at 14:09
  • In your example code you're missing the closing bracket. This ?> should be this: <?php } ?> Commented Nov 3, 2010 at 14:23
  • You might want to search a bit for the term ViewHelper as well. Commented Nov 3, 2010 at 14:28

5 Answers 5

8

Just try it out. But for the record, this works. And is in fact an idiomatic way of solving this.

<?php if ($_SESSION['login']) { ?> 
  <p>Display this HTML code</p>
<?php } else { ?> 
  <p>Display this instead</p>
<?php } ?>

Indented for readability (however, this messes with the HTML structure indentation so maybe it’s not appropriate).

Alternatively, the following style is often used because the lone brace at the end gets lonely:

<?php if ($_SESSION['login']): ?> 
  <p>Display this HTML code</p>
<?php else: ?> 
  <p>Display this instead</p>
<?php endif; ?>

(In both cases I’ve removed the == true from the conditional because it’s utterly redundant. Don’t write == true.)

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

2 Comments

You're missing the <?php at the start of the last line of both those examples...
Ah, too slow! =) Great answer btw, +1
1

you can also use if and endif

<?php if ( expression ) : ?>
<p>some message here</p>
<?php else : ?>
<p>other message</p>
<?php endif ?>

Comments

1

Look into the HEREDOC or NOWDOC syntax

<?php 
if ($_SESSION['login']) { 
   $html =<<<HTML
Add HTML here
HTML;
   echo $html;
} else { 
   $other_html =<<<'OTHERHTML'
Add HTML here
OTHERHTML;
   echo $other_html;
?>

2 Comments

I can't stand the lack of readability with both HEREDOC and NOWDOC syntaxes. To me that just looks like a jumble of code that you need to read line by line to figure out what's going on (Especially since you can't use proper indenting with the syntax). Whereas a traditional structure (as in the question) is much more readable with a quick scan. I'm not going to -1 for it (since it is a valid alternative), but I think it's worth saying that I think it's a bad alternative...
I admit there are some quarks with the HERE/NOW DOC syntax structure, thanx for the constructive criticism as I will keep this in mind in future projects. It's always good to hear honest answers!
1

Anything not in PHP tags will be outputted as HTML anyway, so your original code will work fine.

<?php if ($_SESSION['login'] == true) { ?> 
<a href="logout.html">Log Out</a> 
<?php } else { ?> 
<a href="login.html">Login</a>  
<?php } ?>

Comments

1

Yes, it works.

<?php if ($_SESSION['login'] == true) { ?>
   <span>hello</span>
<?php } else { ?>
   <span>going already?</span>
<?php } ?>

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.