0

I'm trying to pull $username from my database as a greeting. How would I output a variable within my else statement?

The variable username is equal to the following:

$username =  htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');


<?php
if($_GET["p"] == 'login') {
echo "";
} else {
echo "<div class='row'><div class='logo'><img style='margin-left: -21px;' src='http://localhost/ncms/images/logo.png' /></div><p class='panel radius topinfo'>Welcome    . $username .</p>";
}?>
5
  • 1
    What's the problem? You're concatenating when you don't need to, but you should still be seeing the username. Sep 26, 2012 at 20:28
  • Plus, you don't close the <div class='row'>. Did you check the resulting HTML? The username should be there, even if you'll see those concatenating dots around it...
    – Mark
    Sep 26, 2012 at 20:33
  • Are you sure there is a value in $username?
    – pivot
    Sep 26, 2012 at 20:33
  • htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); returns my username, but <?PHP echo $username ?> in my page content returns root. But in my head, it returns nothing. I have not a clue what that is about.
    – Necrohhh
    Sep 26, 2012 at 20:35
  • A bit more investigating, echoing $_SESSION['user']['username'] returns what I want. Still isnt outputting in my <p> tag area
    – Necrohhh
    Sep 26, 2012 at 20:39

4 Answers 4

0

You could use string formatting.

printf("<div class='row'><div class='logo'><img style='margin-left: -21px;' src='http://localhost/ncms/images/logo.png' /></div><p class='panel radius topinfo'>Welcome %s</p>", $username);
0

Alright. Fixed it. Sorry for the confusion to you guys on this one, I had to put the variable inside of my function set that starts the session. Doh!

Sorry again guys and thank you for the tips though I forgot how to concatenate for a second there lol.

0

Php variables auto-expand when used in double quotes, but these are usually used for specifying attribute vales in HTML.

I find the easiest way to remember the rules when echoing HTML is to use single quotes for the HTML, double quotes for the attribute values and string concatonation for php variables.

echo '<div class="row"><div class="logo"><img style="margin-left: -21px;" src="http://localhost/ncms/images/logo.png" /></div></div><p class="panel radius topinfo">Welcome '. $username .'</p>';

You're also missing a closing div (added above).

0

when using the double quote you don't need to concatenate the text string. you can change it to

echo "<div class='row'><div class='logo'><img style='margin-left: -21px;' src='http://localhost/ncms/images/logo.png' /></div><p class='panel radius topinfo'>Welcome $username</p>";

I did not attempt to fix the errors in the code other than the question asked. Validators should be used as suggested in other answers.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.