【问题标题】:How to add variable to value of button?如何将变量添加到按钮的值?
【发布时间】:2014-01-09 06:59:15
【问题描述】:

如何将$num 变量的值添加到按钮的值中?

<?PHP
$num = "3";
    echo '<input id="loadmore" type="button" value="$num" style=" margin-top: 20px; " > <input id="pages" type="hidden" value="$num">';
?>

【问题讨论】:

标签: php button


【解决方案1】:

保持简单,使用.(点)连接变量,

echo '<input id="loadmore" type="button" value="'. $num .'" style=" margin-top: 20px; " > <input id="pages" type="hidden" value="'. $num .'">';

也可以看看heredoc

echo <<<EOT
<input id="loadmore" type="button" value="$num" style=" margin-top: 20px; " > <input id="pages" type="hidden" value="$num">
EOT;

DEMO.

【讨论】:

    【解决方案2】:

    尝试像这样使用。这应该工作

    <?PHP
    $num = "3";
    
    echo '<input id="loadmore" type="button" value="'.$num.'" style=" margin-top: 20px; " > <input id="pages" type="hidden" value="'.$num.'">';
    ?>
    

    【讨论】:

      【解决方案3】:

      当你想使用字符串中的变量时,使用"而不是'来初始化字符串。

      <?PHP
          $num = "3";
          echo "<input id='loadmore' type='button' value='$num' style=' margin-top: 20px;' />     
         <input id='pages' type='hidden' value='$num' />";
      ?>
      

      这会起作用

      【讨论】:

        【解决方案4】:
        <?PHP
            $num = "3";
            echo '<input id="loadmore" type="button" value="'.$num.'" style=" margin-top: 20px; " > 
                <input id="pages" type="hidden" value="'.$num.'">';
        ?>
        

        【讨论】:

        • 如果您在 OP 代码中描述更正,我会很好,它也会对其他人有所帮助。谢谢
        【解决方案5】:

        演示:https://eval.in/87528

        试试这个:

        <?php
        
        $num = "3";
            echo "<input id='loadmore' type='button' value='{$num}' style=' margin-top: 20px; ' > <input id='pages' type='hidden' value='{$num}'>";
        ?>
        

        输出:

        <input id='loadmore' type='button' value='3' style=' margin-top: 20px; ' > <input id='pages' type='hidden' value='3'>
        

        详情请见:http://www.gerd-riesselmann.net/php-beware-of-variables-inside-strings

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多