1

I am having an issue with my php and html. I understand the concept that once <?php ;?> is open you cannot put another line such as <?php ;?> inside that making it look like <?php <?php ;?> ;?> as it throws errors, and if not, it does on my end... Same goes that I understand that using echo, print etc. is more or less what I'm after. My dilemma here is that how would I structure this if using sessions? This is what I have so far, all of which is inside the <body> tag:

<?php echo $_SESSION['id']?'
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
</div>':'
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>';
?>

As you can note, this line: <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2> is what is throwing me errors since it is already inside a previous <?php ;?> statement...

I've tried using <h2>echo "$_SESSION['username'] ? $_SESSION['username'] : 'Guest';"</h2> but that doesn't work either...any help would be much appreciated...

Would I need to create a variable for that session username? So for example:

$username = "<?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?>"; and then place that inside the <h2> element? If so, how would I restructure that to make it work since I've tried that and it throws me errors?

10
  • You realize that you're missing session_start(); Commented Dec 19, 2013 at 20:12
  • no i have that. - i just didn't include what all i have above the html Commented Dec 19, 2013 at 20:12
  • Ok. Had to check. Many are not aware about it and we see a lot of questions related to it. Someone else would've most probably have mentioned the very same thing. Commented Dec 19, 2013 at 20:13
  • no worries, I apologize if what i have isn't worded correctly...but yes, I do have that. My main questions at hand are is how to make what is in the html section, specifically the <h2> elements read whether or not the user is logged in or not and if so reads their username which is already inside php... Commented Dec 19, 2013 at 20:16
  • The <h> tag can accept some additional parameters, from what I've done before, maybe not in conjunction with PHP, but if the intention is to use PHP inside your <h2> tag, then you could probably use <h2 <?php // code >Text</h2> but that's untested of course ;-) Commented Dec 19, 2013 at 20:18

8 Answers 8

2

You have very basic errors in the code while mixing HTML and not closed PHP...

<?php echo $_SESSION['id'] ? 
'<div id="top_mid">
    <h2>'.($_SESSION['username'] ? $_SESSION['username'] : 'Guest').'</h2>
</div>' : '<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>';
?>

So, try to make it easier:

<?php echo $_SESSION['id'] ? $string1 : $string2; ?>

That's the basic conditional. String2 is simple string, nothing really special. String1 contains another conditional, that needs to be separated from string and concated back. To make it simplier:

$string1 = '<div id="top_mid"><h2>';
$string1 .= ($_SESSION['username'] ? $_SESSION['username'] : 'Guest');
$string1 .= '</h2></div>';

Hope this will explain it enough, these are really the basics. It must be obvious to you really fast.

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

4 Comments

This worked! Could you perhaps explain why what is inside the <h2> tag?
It would be greatly appreciated!
Here you go, I hope it'll help you
Thank you very much - with what you posted helped me understand it better!
1

Look at your code. There are basic errors.

<?php if(isset($_SESSION['id'])): ?>
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
</div>
<?php else: ?>
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>
<?php endif; ?>

Comments

1

I personally use the ternary operator where an if statement seems a bit excessive, such as:

echo $animal=="fish"?"Wet":"Dry";

For anything else, an if statement is much easier to read. And echo is not always necessary:

<?php if($_SESSION['id']){ ?>
  <div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
  </div>
<?php } else { ?>
  <h2>
    <li id="toggle" style="list-style: none;">
      <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
      </p>
  </h2>
<?php } ?>

Comments

1

use this instead:

<?php if($_SESSION['id']):?>
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
</div>
<?php else: ?>
    <h2>
        <li id="toggle" style="list-style: none;">
            <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                      <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
            </p>
    </h2>
<?php endif; ?>

Comments

1

The problem is more that you are trying to call <?php ?> inside of another <?php ?>

That said, you need to parse line 3 of your code as just another part of the string and not as a new block of php. Hopefully my edits to your code make enough sense to you

<?php echo $_SESSION['id']?'
<div id="top_mid">
    //modified to just be part of the string:
    <h2>'.($_SESSION['username'] ? $_SESSION['username'] : 'Guest' ).'</h2>
</div>':'
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>';
?>

5 Comments

They do, in fact ejzy had that same format as you with what is inside the <h2> tag. This is what I was looking for as it works, but I don't quite understand why that works, I thought either a print or echo was needed...
It is because you are already echoing a statement. Because you are embedding ternary's (sorry don't know the plural of that word) you dont have to write echo every time you use a one, just before the first. Just think of it as one big if...then... statement. (mind you php has a bunch of notes on ternary's php.net/ternary)
interesting, so if an echo is already in place like how i had it, there is no need to have other echo's inside of it? And having what you have inside the <h2> tag is the correct format for such a thing? I mean, I suppose it is since it works and I'm using it, I just want to make sure this is the best way of coding it in...
Well, what you have is just one long string. and you are interrupting it to put in more data. your middle part there could be simplified like 'this' . $variable . 'That'. except in this case, your variable is just another ternary. in essence it is like saying $variable = ($_SESSION['username'] ? $_SESSION['username'] : 'Guest' ); then in you string <?php echo $_SESSION['id']?' <div...<h2>'.$variable.'</h2>...' does that make sense?
Scratch my comments. See ejzy's. His explanation is much more clear than mine
1

You are missing '>', and i recommend optimizing the code, something like this...

<?php if($_SESSION['id']): ?>
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest'; ?></h2>
</div>
<?php else: ?>
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
           <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>
<?php endif; ?>

Comments

0

(YOU ARE MISSING CLOSING TAG) What about this line then? if your posted code is correct you're missing a basic thing

<?php echo $_SESSION['id']?' <!-- You're missing closing TAG -->

it should be

<?php echo $_SESSION['id'] ?>

and you can't have Another PHP Tag inside the PHP TAG already (before closing it)

2 Comments

to close that up, <?php echo $_SESSION['id']?' I have that included at the very bottom already...
if you want to echo an HTML or put up an if condition at lease use it to close <?php if(isset($_SESSION['id'])): ?>
0

(Presuming that you've already set session_start();)

You can't nest <?echo and ?> like that. Also note the (isset()) around your inline/shorthand if statement to ensure you're actually checking if it's set. The way you had it isn't actually checking the variable for anything. (Not even sure that's valid syntax?)

Do:

<?php echo $_SESSION['id'] ?> <!-- note the closed ?> tag -->
<div id="top_mid">
    <!-- wrapped your $_SESSION['username'] check in (isset()) -->
    <h2><?php echo (isset($_SESSION['username'])) ? $_SESSION['username'] : 'Guest';?></h2>
</div>
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please 
            <a id="open" class="open" href="#open">Login | Register</a>
            <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
    </li> <!-- you were missing this close tag -->
</h2>
<!-- removed the nested closing ?> tag -->

Update, since we're talking about doing conditionals:

<?php

if (isset($_SESSION['id'])) 
{
    echo "<div id='top_mid'>";
    echo "<h2>".(isset($_SESSION['username'])) ? $_SESSION['username'] : 'Guest';."</h2>";
    echo "</div>";
}
else
{
    //...do other stuff
}

?>

...while this approach gets you into dealing with double quotes, single quotes, or escaped quotes, and does add an extra layer to coding (echo EVERYTHING), it keeps your if/else statements a lot cleaner, and if you're using a code highlighter, you can easily see what's being spit out on the page between conditionals or in functions.

4 Comments

Thank you for pointing that out. I completely forgot to close the <li> tag. I do have a question though regarding your setup: The very first line - <?php echo $_SESSION['id'] ?> you closed - does that line apply to what is below? If not, is it even necessary to have it if it is closed right away like that?
php can handle variables from the start of the page to the end; they continue on through the close and open tags -- the tags are only there to denote what is- and isn't php. In your case, if you don't close it, you'll get errors as it tries to parse the html as PHP. Sometimes you need to handle if statements across close tags too (though I've never used them in practice): stackoverflow.com/questions/564130/…
Interesting, so with that being said, wouldn't something like @rybo111 or better yet @webinan's answer be more adequate?
Well, actually yes -- but I honestly didn't understand what you were trying to do with the code. Generally speaking, I find it much cleaner to echo out everything in php. I'll update my answer with a new example.

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.