简体   繁体   中英

Output a variable within an else statement

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>";
}?>

您可以使用字符串格式。

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);

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 your code other than the question asked, please don't downvote. He should be using the validators as suggested above

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM