I want to echo the html inside The variables $p1 $p2 $p3 etc So when i click the button next it goes to next page when i click button previous it goes 1 page back.
This is my working code:
if ($pageShow == 0) {
echo "<center>".$p1."</center>";
} elseif ($pageShow == 1) {
echo "<center>".$p1."</center>";
}elseif ($pageShow == 2) {
echo "<center>".$p2."</center>";
}elseif ($pageShow == 3) {
echo "<center>".$p3."</center>";
}elseif ($pageShow == 4) {
echo "<center>".$p4."</center>";
}elseif ($pageShow == 5) {
echo "<center>".$p5."</center>";
}elseif ($pageShow == 6) {
echo "<center>".$p6."</center>";
}elseif ($pageShow == 7) {
echo "<center>".$p7."</center>";
}elseif ($pageShow == 8) {
echo "<center>".$p8."</center>";
}elseif ($pageShow == 9) {
echo "<center>".$p9."</center>";
}
}
but i wanted to do it like this so it doesnt have that much lines of code:
echo '$p'.$_SESSION['hits'];
So when session is equal to 1 i tought the code would be echo $p1; and when Session is equal to 5 the code would do this echo $p5;
Use single quotes '
for echoing a variable name. eg
echo '$p';
Then do what you did before:
echo '$p'.$_SESSION['hits'];
If $_SESSION['hits']
is 5
, then it will read $p5
.
echo "\$p".$_SESSION['hits'];
this returns as $p5
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
By using the ", PHP is parsing the string and echoing the value of $p
. Instead use ' or escape the dollar sign. Like:
echo '$p' . $_SESSION['hits'];
or
echo "\$p" . $_SESSION['hits'];
If I am understating that you are about to refer a dynamic variable try this
$sessionHits = 'p'.$_SESSION['hits'];
$finalOp = $$sessionHits; // this will output if you have stored something in $p5 , as in this case $_SESSION['hits'] = 5