user3169443
user3169443

Reputation: 11

How to add variable to value of button?

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

Answers (5)

Mahesh
Mahesh

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

Deepika Janiyani
Deepika Janiyani

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

Awlad Liton
Awlad Liton

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

Rikesh
Rikesh

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

niyou
niyou

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

Related Questions