Page 1 of 1

printing variables within a variable

Posted: Tue Jun 07, 2005 11:55 am
by jaymoore_299
is there a way to print the variables within a variable?

Code: Select all

$a="a"
$b="b"
$c="c"
$vars = "text".$a."text".$b."text".$c
print $vars
// so that it outputs: "textatextbtextc"
// just as though I sent it
print "text".$a."text".$b."text".$c;
$vars will be used as a customizable combination of string and variables so that print would show them as though the contents of $vars was sent,

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]

Posted: Tue Jun 07, 2005 12:17 pm
by timvw

Code: Select all

echo "text{$a}text{$b}text{$c}";

Posted: Tue Jun 07, 2005 3:06 pm
by jaymoore_299
The contents of $var will have any number of different combinations of variables that I will not know in advance so that won't work.

Posted: Tue Jun 07, 2005 3:13 pm
by Syranide
best example that works pretty flawlessly (and fast) for this is simple str_replace or preg_replace...

takes a list of "keys", and what they should be changed too.

Posted: Tue Jun 07, 2005 5:43 pm
by timvw
You might want to read http://php.belnet.be/manual/en/functions.arguments.php

It allows you to specify your own function with a variable number of arguments.
Something like (untested)

Code: Select all

function myecho()
{
  foreach(func_get_args() as $name => $val)
  {
    echo "$val";
  }
}

myecho("text", $a, "text", $b, "text", $c);

Posted: Wed Jun 08, 2005 2:56 am
by CoderGoblin
Getting a bit confused with what you are actually asking for.

Code: Select all

$var=&quote;testa&quote;;
$testa=&quote;Hello&quote;;
echo($$var);
who output "Hello". Don't know if that is any use to you.