如何在echo php函数中显示此HTML字符串

如何在echo php函数中显示此HTML字符串,php,echo,Php,Echo,我是php的新手。。。我有一个表单,它将用户名变量传递给php脚本,这是代码。。 Inserisci il tuo nome utente: 我想在botton.php脚本中显示此HTML代码: <a href=www.mysite.com/$username <img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a&

我是php的新手。。。我有一个表单,它将用户名变量传递给php脚本,这是代码。。
Inserisci il tuo nome utente:

我想在botton.php脚本中显示此HTML代码:

<a href=www.mysite.com/$username <img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>

其中$username是从表单传递的变量。。。我怎样才能用回音功能做到这一点?? 谢谢

echo”“;
像这样:

<?php
echo '<a href="http://www.mysite.com/'.$username.'"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>';
?>
<a href="http://www.mysite.com/<?=$username?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>
<a href="http://www.mysite.com/<?=urlencode($_GET['username'])?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>
或者你也可以利用这个机会来使用我上面提到的那些函数(除非你需要$username来做其他用途,然后才能将其呼出)

例如:

$username = urlencode($_GET['username']);
或者你可以直接在回声中这样做:

<?php
echo '<a href="http://www.mysite.com/'.$username.'"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>';
?>
<a href="http://www.mysite.com/<?=$username?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>
<a href="http://www.mysite.com/<?=urlencode($_GET['username'])?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>

您可以将echo用双引号括起来,将html属性用单引号括起来

如果您从表单中获取用户名,请使用以下代码

$username=htmlspecialchars($_请求['username'])

或者,如果分配变量,则使用以下代码

$username=htmlspecialchars(您的文本显示在此处…)

echo”“;
echo sprintf(“”,htmlspecialchars($username,entu引号,'UTF-8');

选项2更好。只需添加
htmlspecialchars()
:)我自己更喜欢选项2。在这种情况下,htmlspcialchars()会比urlencode()更好吗?您可以将
$username
插入
href
属性,因此它是一个URL<代码>(原始)urlencode是这里的方法!:-)哦,我不知道有一个解码。很有趣,谢谢!
echo sprintf('<a href="http://www.mysite.com/%s"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>', htmlspecialchars($username, ENT_QUOTES, 'UTF-8'));