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
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
*/

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.