echo out&#34; <a href="

时间:2016-07-09 10:31:01

标签: php syntax echo

<p>I need to echo this code:</p> <pre><code><a href=" javascript:toggle('test')">="" x="" click.<="" a>="" <="" code="">

I tried it like this:

if (!empty($item['criteria'])) {
    foreach ($item['criteria'] as $item2){
        echo "<a href="javascript:toggle('test')>Click</a>";
        echo '<div id="'. test.'" style="display: none">'. $item2['description'].'</div>';
    }
}

I think there is a mistake with the "".

2 个答案:

答案 0 :(得分:0)

你正在寻找这样的事情:

<?php
if (!empty($item['criteria'])) {
    foreach ($item['criteria'] as $item2) {
        echo "<a href=\"javascript:toggle('test');\">Click</a>";
        echo '<div id="' . 'test' . '" style="display: none">'. $item2['description'].'</div>';
    }
}

你需要正确地逃避字符串中的引号。

答案 1 :(得分:0)

如果您要在包含相同类型引号的字符串中使用引号,例如&#34;东西&#34; &#34;然后你需要用\来转义字符串中的引号,所以在你的情况下:

if(!empty($item['criteria'])){
            foreach ($item['criteria'] as $item2){
             echo "<a href=\"javascript:toggle('test')\">Click</a>";
             echo '<div id="'. 'test' .'" style="display: none">'. $item2['description'].'</div>';
            }
        }
}

您也忘了用第二个&#34;

关闭href