Jump to content

[SOLVED] php Variable questions


raul_7

Recommended Posts

Hello everyone,

thank you very much for all the help you guys provide i have a question

lets say i have bunch of variables like this

$test1 $test2 $test3 etc ... now i want to print each variable but i dont want to type $test1 $test2 every time (there are over 200 variable) and each time user selects different number of variables, so i have a for loop which makes a $i variable and increase it every time it runs, i was hoping to see if there is anyway to print bunch of them maybe like this ?

echo $test$i; -->of course this doesn't work but i am just trying to show what i need. any help is very much appreciated.

 

Thank you.

 

Raul

Link to comment
https://forums.phpfreaks.com/topic/61988-solved-php-variable-questions/
Share on other sites

Because you are just concatenating two variables.

 

Your code:

$test = 'default';
$test1 = 'a';
$test2 = 'b';
$test3 = 'c';

for($i = 1; $i <= 3; $i++)
{
    echo $test . $i;
}
/*
Output:
default1
default2
default3
*/

 

My code:

$test = 'default';
$test1 = 'a';
$test2 = 'b';
$test3 = 'c';

for($i = 1; $i <= 3; $i++)
{
    eval('echo $test'. $i .';');
}
/*
Output:
a
b
c
*/

<?php

$test1 = "=)";
$test2 = "";
$test3 = "";
$test4 = ":|";
$test5 = ":/";

if( ($face = intval($face)) >= 1 && ($face = intval($face)) <= 5){
  
  $myvar = "test$face";
  
  $myvalue = $$myvar;
  
  echo "<div><b>$myvalue</b></div>";
}

?><form action="<?php echo $PHP_SELF; ?>" method="get">
<div>Enter 1-5 <input type="text" name="face"></div>
<div><input type="submit" value="Get my Face!"></div>
</form>

Nakor your code doesnt work... :

 

Error:

Parse error: syntax error, unexpected $end, expecting ',' or ';' in /home/dreamsh/public_html/test.php(9) : eval()'d code on line 1

Parse error: syntax error, unexpected $end, expecting ',' or ';' in /home/dreamsh/public_html/test.php(9) : eval()'d code on line 1

Parse error: syntax error, unexpected $end, expecting ',' or ';' in /home/dreamsh/public_html/test.php(9) : eval()'d code on line 1

 

Code:

<?php
$test = 'default';
$test1 = 'a';
$test2 = 'b';
$test3 = 'c';

for($i = 1; $i <= 3; $i++)
{
    eval('echo $test'. $i);
}
?>

Here it is written using eval.

 

<?php

$test1 = "=)";
$test2 = "";
$test3 = "";
$test4 = ":|";
$test5 = ":/";

if( ($face = intval($face)) >= 1 && ($face = intval($face)) <= 5){
  
  eval("\$x = \$test$face;");
  
  $myvalue = $x;
  
  echo "<div><b>$myvalue</b></div>";
}

?><form action="<?php echo $PHP_SELF; ?>" method="get">
<div>Enter 1-5 <input type="text" name="face"></div>
<div><input type="submit" value="Get my Face!"></div>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.