How to concatenate variable and string in following scenario in PHP?

自作多情 提交于 2019-12-04 20:51:48

String concatenation

<?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>";
?>

OR If you want to use the Zend specification

<?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>";
    ?>

You started with double quotes, so you can do it this way:

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

or use curly braces {} around the variable:

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

Both are valid.

Try this:

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

when you are concating the string you need use double quotes since you started string with double quotes.

the problem lies with $rebate_no[1]. you can either store the value of that into a regular variable, append the value to the string like below, or surround it in curly brackets like so {$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>";

you got the trick wrong.

instead of doing

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

do

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

There is easiest method Use (" ") double quotes instead of (' ') without making it complicated.

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

instead of

<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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!