带引号的HTML / PHP代码

时间:2017-02-06 18:20:49

标签: php html

我有一个我正在开发的代码,但现在我被困,因为我是 PHP 的新手。 当我使用简单的引号时它可以工作但是,我认为这段代码有点难看和奇怪,我希望有人来复习。

代码:

<?php echo"
                    <li class='light-blue'>
                        <a data-toggle='dropdown' href='#' class='dropdown-toggle'>
                            <img class='nav-user-photo' src='../upload/avatar/$arqDir'>".$_SESSION['img'].">
                            <span class='user-info'>
                                <small>Bem Vindo,</small>       
                                    echo ".$_SESSION['nome']."; 
                            </span>

                            <i class='ace-icon fa fa-caret-down'></i>
                        </a>

                        <ul class='user-menu dropdown-menu-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close'>
                            <li>
                                <a href='#'>
                                    <i class='ace-icon fa fa-cog'></i>
                                    Cofigurações
                                </a>
                            </li>

                            <li>
                                <a href='profile.html'>
                                    <i class='ace-icon fa fa-user'></i>
                                    Perfil
                                </a>
                            </li>

                            <li class='divider'></li>

                            <li>
                                <a href='logout.php'>
                                    <i class='ace-icon fa fa-power-off'></i>
                                    Sair
                                </a>
                            </li>
                        </ul>
                    </li>";
                    ?>

3 个答案:

答案 0 :(得分:0)

为了消除任何混淆,请在echo语句中使用双引号并使用必要的转义。例如

echo "<div class=\"boy\"> </div>";

OR

echo"<li class=\"boy\"> </li>";

班级名称&#39;男孩&#39;也有双引号,所以我们只是用反弹来逃脱它。 如果您想使用数组,只需在需要时关闭并打开引号。例如

echo "The user is: ".$row['user'].":";

以上行将输出如下内容: 用户是:user:

<?php echo"
<li class=\"light-blue\">
<a data-toggle=\"dropdown\" href=\"#\" class=\"dropdown-toggle\">
<img class=\"nav-user-photo\" src=\"../upload/avatar/$arqDir\"".$_SESSION['img'].">
                            <span class=\"user-info\">
                                <small>Bem Vindo,</small>       
                                     ".$_SESSION['nome']."; 
                            </span>

                            <i class=\"ace-icon fa fa-caret-down\"></i>
                        </a>

                        <ul class=\"user-menu dropdown-menu-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close\">
                            <li>
                                <a href=\"#\">
                                    <i class=\"ace-icon fa fa-cog\"></i>
                                    Cofigurações
                                </a>
                            </li>

                            <li>
                                <a href=\"profile.html\">
                                    <i class=\"ace-icon fa fa-user\"></i>
                                    Perfil
                                </a>
                            </li>

                            <li class=\"divider\"></li>

                            <li>
                                <a href=\"logout.php\">
                                    <i class=\"ace-icon fa fa-power-off\"></i>
                                    Sair
                                </a>
                            </li>
                        </ul>
                    </li>";
                    ?>

我试图相应地检查和编辑代码。我希望你能得到这个。

答案 1 :(得分:0)

正如AbraCadaver所说,这是可以做到的一种方式:

<li class="light-blue">
    <a data-toggle="dropdown" href="#" class="dropdown-toggle">
        <img class="nav-user-photo" src="../upload/avatar/<?php echo $arqDir . "/" . $_SESSION['img'];?>">
        <span class="user-info">
            <small>Bem Vindo,</small>       
                <?php echo $_SESSION['nome']; ?>
        </span>

        <i class="ace-icon fa fa-caret-down"></i>
    </a>

    <ul class="user-menu dropdown-menu-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close">
        <li>
            <a href="#">
                <i class="ace-icon fa fa-cog"></i>
                Cofigurações
            </a>
        </li>

        <li>
            <a href="profile.html">
                <i class="ace-icon fa fa-user"></i>
                Perfil
            </a>
        </li>

        <li class="divider"></li>

        <li>
            <a href="logout.php">
                <i class="ace-icon fa fa-power-off"></i>
                Sair
            </a>
        </li>
    </ul>
</li>

另一种方式可能是:

<?php 
    echo '

    <li class="light-blue">
    <a data-toggle="dropdown" href="#" class="dropdown-toggle">
        <img class="nav-user-photo" src="../upload/avatar/'. $arqDir . '/' . $_SESSION['img'].'">
        <span class="user-info">
            <small>Bem Vindo,</small>       
                '.$_SESSION['nome'].'
        </span>

        <i class="ace-icon fa fa-caret-down"></i>
    </a>

    <ul class="user-menu dropdown-menu-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close">
        <li>
            <a href="#">
                <i class="ace-icon fa fa-cog"></i>
                Cofigurações
            </a>
        </li>

        <li>
            <a href="profile.html">
                <i class="ace-icon fa fa-user"></i>
                Perfil
            </a>
        </li>

        <li class="divider"></li>

        <li>
            <a href="logout.php">
                <i class="ace-icon fa fa-power-off"></i>
                Sair
            </a>
        </li>
    </ul>
</li>';

答案 2 :(得分:0)

据我所知,三种方法是:
 1. <a href="google.com"> <?php echo $variable; ?> </a>
 2.

<?php 
   echo "<a href=\"google.com\"> $variable </a>";
?>

3

<?php 
echo <<< LINK 
<a href="google.com"> $variable </a> 
LINK; 
... 
?>

我更喜欢第三种解决方案。在我看来是最清楚的 在你的情况下应该看起来像:

echo <<< html
<li class='light-blue'>
                    <a data-toggle='dropdown' href='#' class='dropdown-toggle'>
                        <img class='nav-user-photo' src='../upload/avatar/$arqDir'>$_SESSION[img]>
                        <span class='user-info'>
                            <small>Bem Vindo,</small>       
                                $_SESSION[nome] 
                        </span>

                        <i class='ace-icon fa fa-caret-down'></i>
                    </a>

                    <ul class='user-menu dropdown-menu-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close'>
                        <li>
                            <a href='#'>
                                <i class='ace-icon fa fa-cog'></i>
                                Cofigurações
                            </a>
                        </li>

                        <li>
                            <a href='profile.html'>
                                <i class='ace-icon fa fa-user'></i>
                                Perfil
                            </a>
                        </li>

                        <li class='divider'></li>

                        <li>
                            <a href='logout.php'>
                                <i class='ace-icon fa fa-power-off'></i>
                                Sair
                            </a>
                        </li>
                    </ul>
                </li>
html;