如何在PHP中的以下场景中连接变量和字符串?

时间:2014-05-05 07:33:40

标签: php html concatenation string-concatenation

我遵循PHP代码:

<?php
$rebate_no = 2;
echo "<table id='blacklistgrid_$rebate_no'  class='table table-bordered table-hover table-striped blacklistgrid'>
        <tr id='reb$rebate_no_1'>
          <td>
            <div class='btn-group'>
              <select name='product_id_$rebate_no[1]' id='product_id_$rebate_no_1' class='form-control prod_list'>
                <option value='1'>Alabama</option>
                <option value='2'>Alaska</option>
                <option value='3'>Arizona</option>
                <option value='4'>Arkansas</option>
                <option value='5'>California</option>
              </select>
            </div>
          </td>
        </tr>
      </table>";
?>

在上面的代码中,我遇到了以下几行连接变量和字符串的问题:

<tr id='reb$rebate_no_1'>
<select name='product_id_$rebate_no[1]' id='product_id_$rebate_no_1' class='form-control prod_list'>

我尝试了以下技巧,但它对我不起作用。

<tr id='reb'.$rebate_no.'_1'>
<select name='product_id_$rebate_no[1]' id='product_id_'.$rebate_no.'_1' class='form-control prod_list'>

如果我签入HTML源代码,我将关注HTML:

<tr id="reb" .2.'_1'="">
<select id="product_id_" class="form-control prod_list" .2.'_1'="" name="product_id_">

实际上我希望HTML符合以下格式:

<tr id='reb2_1'>
<select name='product_id_2[1]' id='product_id_2_1' class='form-control prod_list'>

如何实现这一目标?提前谢谢。

6 个答案:

答案 0 :(得分:2)

字符串连接

<?php
$rebate_no = 2;
echo "<table id='blacklistgrid_".$rebate_no."'  class='table table-bordered table-hover table-striped blacklistgrid'>
        <tr id='reb".$rebate_no_1."'>
          <td>
            <div class='btn-group'>
              <select name='product_id_".$rebate_no[1]." id='product_id_".$rebate_no_1."' class='form-control prod_list'>
                <option value='1'>Alabama</option>
                <option value='2'>Alaska</option>
                <option value='3'>Arizona</option>
                <option value='4'>Arkansas</option>
                <option value='5'>California</option>
              </select>
            </div>
          </td>
        </tr>
      </table>";
?>

如果你想使用Zend规范

<?php
    $rebate_no = 2;
    echo "<table id='blacklistgrid_{$rebate_no}'  class='table table-bordered table-hover table-striped blacklistgrid'>
            <tr id='reb{$rebate_no_1}'>
              <td>
                <div class='btn-group'>
                  <select name='product_id_{$rebate_no[1]} id='product_id_{$rebate_no_1}' class='form-control prod_list'>
                    <option value='1'>Alabama</option>
                    <option value='2'>Alaska</option>
                    <option value='3'>Arizona</option>
                    <option value='4'>Arkansas</option>
                    <option value='5'>California</option>
                  </select>
                </div>
              </td>
            </tr>
          </table>";
    ?>

答案 1 :(得分:1)

你从双引号开始,所以你可以这样做:

<tr id='reb".$rebate_no."_1'>

或在变量周围使用花括号{}

<tr id='reb{$rebate_no}_1'>

两者都有效。

答案 2 :(得分:1)

试试这个:

   <tr id='re'".$rebate_no."_1'>
    <select name='product_id_$rebate_no[1]' id='product_id_".$rebate_no."_1' class='form-control prod_list'>

当你在串联字符串时,你需要使用双引号,因为你用双引号开始字符串。

答案 3 :(得分:1)

问题在于$rebate_no[1]。你可以将它的值存储到常规变量中,将值附加到字符串中,如下所示,或者用大括号括起来,如{$rebate_no[1]}

echo "<table id='blacklistgrid_$rebate_no'  class='table table-bordered table-hover table-striped blacklistgrid'>
        <tr id='reb$rebate_no_1'>
          <td>
            <div class='btn-group'>
              <select name='product_id_" . $rebate_no[1] . "' id='product_id_$rebate_no_1' class='form-control prod_list'>
                <option value='1'>Alabama</option>
                <option value='2'>Alaska</option>
                <option value='3'>Arizona</option>
                <option value='4'>Arkansas</option>
                <option value='5'>California</option>
              </select>
            </div>
          </td>
        </tr>
      </table>";

答案 4 :(得分:1)

你弄错了。

而不是

<tr id='reb'.$rebate_no.'_1'>

<tr id='reb".$rebate_no."_1'>

答案 5 :(得分:1)

最简单的方法是使用(“”)双引号而不是(''),而不会使它变得复杂。

<tr id="reb$rebate_no_1">
<select name="product_id_$rebate_no[1]" id="product_id_$rebate_no_1" class='form-control prod_list'>

而不是

<tr id='reb$rebate_no_1'>
<select name='product_id_$rebate_no[1]' id='product_id_$rebate_no_1' class='form-control prod_list'>

Reff:PHP: different quotes?