【问题标题】:Use html in a php variable在 php 变量中使用 html
【发布时间】:2016-09-25 11:58:52
【问题描述】:

我在带有 html 的 php 中有这个变量,但它不起作用。你们能找到任何错误吗?我试过了,但没有找到任何东西。 代码是:

$html .= '<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
            <div class="box-produtos">                
                <a href="' . $url_imagem . '" class="magnifier" rel="shadowbox">
                    <img src="' . $url_imagem . '" class="img-responsive img-produtos center-block" onerror="imgError(this);">
                </a>

            <div class="facebook-btn" onclick="window.open("http://www.facebook.com/sharer/sharer.php?u=' . $base_url . '"/facebookPublish.php?idproduto="' . $idproduto . '", "sharer", "toolbar=0,status=0,width=548,height=325");"><i class="fa fa-facebook fc-facebook"></i>' . $lang["FACEBOOK_SHARE"] . '</div>

            <div class="clearfix"></div>             
            <a href="' . $base_url . '/' . $langi . '/produtosDesc/' . $idproduto . '"><h5 class="prod-size">' . $produto . '<br>' . $refproduto . '</h5></a>                
            <div class="clearfix"></div>
            <h5 class="text-center font-color">' . $lang['DEFAULT_PRICE'] . ' ' . $preco . ' &euro;</h5>
            <a href="' . $base_url . '/' . $langi . '/produtosDesc/' . $idproduto . '" class="btn btn-skin center-block btn-prod">' . $lang['GERAL_COMPRAR'] . '</a>';     

【问题讨论】:

  • 这是一种不好的做法,将它们分开,单独使用 PHP 和 HTML。
  • 我的老板要我做这样的
  • 干脆不要像"' . $url_imagem . '"那样使用串联,像"$url_imagem"这样使用
  • @MiniKing17-Tiago 它怎么不工作?
  • 你没有关闭第一个和第二个 div。

标签: php html


【解决方案1】:

你不能将一个字符串分布在多行上。(Justinas 说你可以。见下面的comment

我认为这个问题与您的问题有关: Best Practices: working with long, multiline strings in PHP?

编辑 1:

我认为另一种解决方案也可能是您将 HTML 放在一个单独的文件中,然后将其内容读取到您的变量中。

$html .= file_get_contents("your-file.html");

编辑 2: 删除了错误的陈述。如Justinas comment中所述。

【讨论】:

  • 为什么你认为你不能?这不是 JS。在 JS 中,每行只能有一个字符串,并且必须连接起来。 关于编辑你将如何将 PHP var 放在这个 $html 中而不是?
  • 当然。您不能简单地在其中使用 php 变量。在我的情况下,我不需要这样做。如果有人遇到与我相同的问题,我认为解决方案可能会有所帮助。
【解决方案2】:

如果你想避免引号的错误,你可以试试这样:

$var="variables";
$html =<<<HTML
<h1>All your html code</h1>
<p>with all your $var or {$var}</p>
HTML;

echo $html;

这样更安全。

【讨论】:

    【解决方案3】:

    您可以使用Heredocs 以漂亮的方式混合 PHP 和 HTML。

    $html .= <<< HTML
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
            <div class="box-produtos">                
                <a href="{$url_imagem}" class="magnifier" rel="shadowbox">
                    <img src="{$url_imagem}" class="img-responsive img-produtos center-block" onerror="imgError(this);">
                </a>
    
                <div class="facebook-btn" onclick="window.open("http://www.facebook.com/sharer/sharer.php?u={$base_url}/facebookPublish.php?idproduto={$idproduto}", "sharer", "toolbar=0,status=0,width=548,height=325");">
                    <i class="fa fa-facebook fc-facebook"></i>{$lang["FACEBOOK_SHARE"]}
                </div>
    
                <div class="clearfix"></div>             
                <a href="{$base_url}/{$langi}/produtosDesc/{$idproduto}">
                    <h5 class="prod-size">{$produto}<br/>{$refproduto}</h5>
                </a>                
                <div class="clearfix"></div>
                <h5 class="text-center font-color">
                    {$lang['DEFAULT_PRICE']} {$preco} &euro;
                </h5>
                <a href="{$base_url}/{$langi}/produtosDesc/{$idproduto}" class="btn btn-skin center-block btn-prod">
                    {$lang['GERAL_COMPRAR']}
                </a>
    HTML;// no indentation, must be single in line, so remove this comment
    

    你还弄乱了onclick 属性中的 facebook 链接。

    【讨论】:

      【解决方案4】:

      在指定 php 变量时使用&lt;?php ?&gt;

      【讨论】:

        猜你喜欢
        • 2014-06-04
        • 1970-01-01
        • 2022-01-26
        • 2013-09-08
        • 2012-11-03
        • 2014-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多