1

How to add value of $num variable to value of button ?

<?PHP
$num = "3";
    echo '<input id="loadmore" type="button" value="$num" style=" margin-top: 20px; " > <input id="pages" type="hidden" value="$num">';
?>
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • variables in string with single quote will not be parsed & substituted with value. In short, single quote = no variable inside; double quote = can have variable inside – Raptor Jan 09 '14 at 07:02
  • possible duplicate of [PHP - concatenate or directly insert variables in string](http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string) – Peon Jan 09 '14 at 07:16

5 Answers5

6

Keep it simple, use . (dot) to concate variable,

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

Also have a look at heredoc,

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

DEMO.

Rikesh
  • 26,156
  • 14
  • 79
  • 87
3

try to use like this. this should work

<?PHP
$num = "3";

echo '<input id="loadmore" type="button" value="'.$num.'" style=" margin-top: 20px; " > <input id="pages" type="hidden" value="'.$num.'">';
?>
Mahesh
  • 872
  • 1
  • 10
  • 25
1

Whenever you want to use, variables inside a string, use " instead of ' to initialize the string.

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

this will work

Deepika Janiyani
  • 1,487
  • 9
  • 17
0
<?PHP
    $num = "3";
    echo '<input id="loadmore" type="button" value="'.$num.'" style=" margin-top: 20px; " > 
        <input id="pages" type="hidden" value="'.$num.'">';
?>
niyou
  • 875
  • 1
  • 11
  • 23
0

Demo : https://eval.in/87528

Try this:

<?php

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

Output:

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

See this for details: http://www.gerd-riesselmann.net/php-beware-of-variables-inside-strings

Awlad Liton
  • 9,366
  • 2
  • 27
  • 53