简体   繁体   English

在else语句中输出变量

[英]Output a variable within an else statement

I'm trying to pull $username from my database as a greeting. 我试图从数据库中拉出$username作为问候。 How would I output a variable within my else statement? 如何在else语句中输出变量?

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. Php变量在双引号中使用时会自动扩展,但通常用于在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. 我发现在回显HTML时记住规则的最简单方法是对HTML使用单引号,对属性值使用双引号,对php变量使用字符串组合。

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). 您还缺少关闭div(在上面添加)。

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! h!

Sorry again guys and thank you for the tips though I forgot how to concatenate for a second there lol. 再次抱歉,伙计们,感谢您的提示,尽管我忘记了如何在那儿连接一秒钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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