Reputation: 11
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">';
?>
Upvotes: 1
Views: 760
Reputation: 870
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.'">';
?>
Upvotes: 3
Reputation: 1477
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
Upvotes: 1
Reputation: 9351
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
Upvotes: 0
Reputation: 26431
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.
Upvotes: 6
Reputation: 873
<?PHP
$num = "3";
echo '<input id="loadmore" type="button" value="'.$num.'" style=" margin-top: 20px; " >
<input id="pages" type="hidden" value="'.$num.'">';
?>
Upvotes: 0