【问题标题】:String interpolation with an array使用数组进行字符串插值
【发布时间】:2012-08-06 01:47:37
【问题描述】:

我正在尝试使用 php 创建链接,但遇到了一些困难。有人可以帮我弄这个吗。我希望链接转到 yourteam.php,标题是变量 $row['User_ID'] 是什么......

echo "<tr bgcolor=\"#D1D1D1\"><td>" . "<a href=\"yourteam.php\">$row['User_ID']</a>" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";

【问题讨论】:

  • html 输出是什么?

标签: php


【解决方案1】:

在双引号字符串中使用数组项时,您会省略任何引号

echo "<tr bgcolor=\"#D1D1D1\"><td>" . "<a href=\"yourteam.php\">$row[User_ID]</a>" .

您也可以将变量包装在{}

echo "<tr bgcolor=\"#D1D1D1\"><td>" . "<a href=\"yourteam.php\">{$row['User_ID']}</a>" .

【讨论】:

    【解决方案2】:

    试试这个

    echo "<tr bgcolor='#D1D1D1'><td><a href='yourteam.php'>".$row['User_ID']."</a></td><td><b>" . $row['Correct_Picks'] . "</b>" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
    

    我建议你使用&lt;strong&gt;&lt;/strong&gt; 而不是&lt;b&gt;&lt;/b&gt;,你也可以使用内联样式style='background-color:#D1D1D1;' 而不是bgcolor

    【讨论】:

      【解决方案3】:

      我知道有人建议使用单引号,但我喜欢在我的 HTML 属性上使用双引号,并在我的 echo 语句中使用缩进以提高可读性。这就是我的做法......

      echo "<tr bgcolor=\"#D1D1D1\">";
      echo "    <td><a href=\"yourteam.php\">{$row['User_ID']}</a></td>";
      echo "    <td><b>{$row['Correct_Picks']}</b>{$maxcorrectpicks}</td>";
      echo "    <td>{$row['Points']}</td>";
      echo "</tr>";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-12
        • 1970-01-01
        • 2016-06-05
        • 1970-01-01
        • 1970-01-01
        • 2020-03-30
        • 2012-01-04
        • 1970-01-01
        相关资源
        最近更新 更多