/    Sign up×
Community /Pin to ProfileBookmark

join variable function ${‘text’. $i}

Hi, in my old website I found this function that is used to create new variable based on string and variable….

[code=php]
$i=”1″;
$a=${‘text’. $i};

echo $a; //returns text1

[/code]

This function is used fot DB cycling…. exept it doesnt work for some reason on a new server… so can somebody explane me why isnt it working and maybe give me a link to documentation?

Thanks!

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanJan 13.2005 — I'm not exactly wshure what your doing but why don't you try and use [url=http://www.php.net/eval]eval()[/url]?


If you want the link to the PHP documentation then have a look in my signature.


RyanJ
Copy linkTweet thisAlerts:
@ShrineDesignsJan 13.2005 — try:[code=php]<?php
$i = 1;
eval("$text$i = 'foobar';");
echo $text1;
?>[/code]
Copy linkTweet thisAlerts:
@NogDogJan 13.2005 — [i]Originally posted by ShrineDesigns [/i]

[B]try:[code=php]<?php
$i = 1;
eval("$text$i = 'foobar';");
echo $text1;
?>[/code]
[/B][/QUOTE]

Better yet, try:
[code=php]
$i = 1;
$text[$i] = 'foobar';
echo $text[$i];
[/code]

?
Copy linkTweet thisAlerts:
@alexusauthorJan 13.2005 — Ok, I guess I'll have to use eval();

but then how can I properly cycle this to ptint txt$i:

[code=php]
$txt1="1";
$txt2="2";
$txt3="3";
$txt4="4";
$txt5="5";
$txt6="6";
$txt7="7";

while(?????){
$str = 'txt$i';
eval("$str = "$str";");
echo $str;
$i++;
}
[/code]
Copy linkTweet thisAlerts:
@pyroJan 13.2005 — What exactly are you trying to do here? From the example you gave, using an array would make a heck of a lot more sense, as you could just iterate through the array to echo out the values.
Copy linkTweet thisAlerts:
@NogDogJan 13.2005 — [code=php]
# assume there are variables $text1 - $text99
for($i=1;$i<=99;$i++)
{
eval("\$text$i = \"foobar\";");
}
[/code]
Copy linkTweet thisAlerts:
@alexusauthorJan 13.2005 — what if I dont what is the number of $txt$i (that is why i needed it) yhe numbet may vary ,,,
Copy linkTweet thisAlerts:
@NogDogJan 13.2005 — [i]Originally posted by alexus [/i]

[B]what if I dont what is the number of $txt$i (that is why i needed it) yhe numbet may vary ,,, [/B][/QUOTE]

[code=php]
$ix = 1;
while(TRUE) ### DANGER, WILL ROBINSON!!!!!
{
eval("if(!isset(\$text$ix)) { break ; }");
eval("\$text$ix = \"foobar\";");
$ix++;
}
[/code]
Copy linkTweet thisAlerts:
@alexusauthorJan 16.2005 — NogDog, your example returns an error...

Well any way I got my function to work the way I need it.

Here is the code:
[code=php]
$txt1='1';
$txt2='2';
$txt3='3';
$txt4='4';
$txt5='5';
$txt6='6';
$txt7='7';

$i=1;
while( isset(${'txt' .$i})){
echo ${'txt' .$i};
$i++;
}
[/code]


This will return: "1234567"

I thin this function is way more easy to use rather then eval()
×

Success!

Help @alexus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...