Connecting Tech Pros Worldwide Forums | Help | Site Map

how to combine the results of two variables to echo another variable?

Dave
Guest
 
Posts: n/a
#1: Jul 17 '05
Hello,
I'm a somewhat PHP newbie, so please bear with me.

Here is what I am trying to do:

$foobar = "blah blah";
$a = "foo";
$b = "bar";
$fooboo = "$a" . "$b";
echo $fooboo

I know my syntax here is not correct so I am looking for the function
to make $fooboo echo out the $foobar variable "blah blah", and not as
"foobar".

Any help is appreciated.


Sacs
Guest
 
Posts: n/a
#2: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Dave wrote:[color=blue]
> Hello,
> I'm a somewhat PHP newbie, so please bear with me.
>
> Here is what I am trying to do:
>
> $foobar = "blah blah";
> $a = "foo";
> $b = "bar";
> $fooboo = "$a" . "$b";
> echo $fooboo
>
> I know my syntax here is not correct so I am looking for the function
> to make $fooboo echo out the $foobar variable "blah blah", and not as
> "foobar".
>
> Any help is appreciated.
>[/color]

Variable variables:
http://nz.php.net/manual/en/language...s.variable.php

$foobar = "blah blah";
$a = "foo";
$b = "bar";
$fooboo = "$a" . "$b";

$fooboo = $$fooboo;

echo $fooboo

Sacs
Ewoud Dronkert
Guest
 
Posts: n/a
#3: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Sacs wrote:[color=blue]
> Variable variables:
> http://www.php.net/manual/en/languag...s.variable.php[/color]

Yes. But why......:
[color=blue]
> $fooboo = "$a" . "$b";[/color]

Just do: $fooboo = $a . $b;


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Sacs
Guest
 
Posts: n/a
#4: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Ewoud Dronkert wrote:[color=blue]
> Sacs wrote:
>[color=green]
>>Variable variables:
>>http://www.php.net/manual/en/languag...s.variable.php[/color]
>
>
> Yes. But why......:
>[/color]

Variable variables are one of the most powerfull features of php I've
found. Very usefull when you dont know how many variables to be
processed in a form for e.g.

for( $i=1; $i <= $num_fields; $i++) {
$blah = "field_" . $i;
// $blah is now "field_1" first time through the loop

$blah = $$blah;
// now $blah holds what was input into the appropriate input
}

echo '<form>
<input type=text name="field_1">
<input type=text name="field_2">
<input type=text name="field_3">
<input type=hidden name="num_fields" value=3>
<input type=submit>
</form>';

[color=blue]
>[color=green]
>>$fooboo = "$a" . "$b";[/color]
>
>
> Just do: $fooboo = $a . $b;
>[/color]
With just $fooboo = $a . $b; then $fooboo is set to "foobar" not "blah
blah" as op wanted.

Sacs
Ewoud Dronkert
Guest
 
Posts: n/a
#5: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Sacs wrote:[color=blue][color=green][color=darkred]
>>>$fooboo = "$a" . "$b";[/color]
>>
>> Just do: $fooboo = $a . $b;
>>[/color]
> With just $fooboo = $a . $b; then $fooboo is set to "foobar" not "blah
> blah" as op wanted.[/color]

Sigh. I meant: why use the quotes, they're not needed! (and yes, then use
$$fooboo, just like in the code a few posts up).


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Philip Olson
Guest
 
Posts: n/a
#6: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Or simply use arrays instead (preferred by most):

<form method="POST">
<input type=text name="field[]">
<input type=text name="field[]">
<input type=text name="field[foo]">
<input type=submit>
</form>

foreach ($_POST['field'] as $key => $value) {
...
}

http://php.net/manual/en/faq.html.php#faq.html.arrays

BKDotCom
Guest
 
Posts: n/a
#7: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Ewoud Dronkert wrote:[color=blue]
> Sigh. I meant: why use the quotes, they're not needed! (and yes, then[/color]
use[color=blue]
> $$fooboo, just like in the code a few posts up).[/color]

It seems the PHP community likes to put quotes around variables.
It's baffeling.

Sacs
Guest
 
Posts: n/a
#8: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Philip Olson wrote:[color=blue]
> Or simply use arrays instead (preferred by most):
>
> <form method="POST">
> <input type=text name="field[]">
> <input type=text name="field[]">
> <input type=text name="field[foo]">
> <input type=submit>
> </form>
>
> foreach ($_POST['field'] as $key => $value) {
> ...
> }
>
> http://php.net/manual/en/faq.html.php#faq.html.arrays
>[/color]
Where's the fun in that! My way was much harder to comprehend, must be
perl haunting me....

Actually, never thought of passing back an array in a form... hmmm....

:-)
Sacs
Sacs
Guest
 
Posts: n/a
#9: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


Ewoud Dronkert wrote:[color=blue]
> Sigh. I meant: why use the quotes, they're not needed! (and yes, then use
> $$fooboo, just like in the code a few posts up).[/color]


Oh. Wrong end of stick. :-)

And I don't know why so many people put quotes around variables in PHP.
*shrug*

Sacs
Hannes Dorbath
Guest
 
Posts: n/a
#10: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


There is no need for variable variables at all. Use arrays for stuff
like this. Yeah, v-vars are indeed powerfull, powerfull to lead ppl to
unpredictable clutter up the root of the namespace and write ugly code
-- like demonstrated below in all beauty.

On 29.04.2005 00:51, Sacs wrote:[color=blue]
> Ewoud Dronkert wrote:
>
> Variable variables are one of the most powerfull features of php I've
> found. Very usefull when you dont know how many variables to be
> processed in a form for e.g.
>
> for( $i=1; $i <= $num_fields; $i++) {
> $blah = "field_" . $i;
> // $blah is now "field_1" first time through the loop
>
> $blah = $$blah;
> // now $blah holds what was input into the appropriate input
> }
>[/color]
Ewoud Dronkert
Guest
 
Posts: n/a
#11: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


On 28 Apr 2005 16:27:31 -0700, BKDotCom wrote:[color=blue]
> It seems the PHP community likes to put quotes around variables.
> It's baffeling.[/color]

Indeed. Like, echo "$aaargh";


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Ewoud Dronkert
Guest
 
Posts: n/a
#12: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


On Fri, 29 Apr 2005 11:40:01 +1200, Sacs wrote:[color=blue]
> Oh. Wrong end of stick. :-)[/color]

Yeah, sorry about the sigh, it was late.


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Norman Peelman
Guest
 
Posts: n/a
#13: Jul 17 '05

re: how to combine the results of two variables to echo another variable?


"Sacs" <alan_nospam_@way.co.nz> wrote in message
news:3Odce.2081$Od6.321012@news.xtra.co.nz...[color=blue]
> Ewoud Dronkert wrote:[color=green]
> > Sacs wrote:
> >[color=darkred]
> >>Variable variables:
> >>http://www.php.net/manual/en/languag...s.variable.php[/color]
> >
> >
> > Yes. But why......:
> >[/color]
>
> Variable variables are one of the most powerfull features of php I've
> found. Very usefull when you dont know how many variables to be
> processed in a form for e.g.
>
> for( $i=1; $i <= $num_fields; $i++) {
> $blah = "field_" . $i;
> // $blah is now "field_1" first time through the loop
>
> $blah = $$blah;
> // now $blah holds what was input into the appropriate input
> }
>
> echo '<form>
> <input type=text name="field_1">
> <input type=text name="field_2">
> <input type=text name="field_3">
> <input type=hidden name="num_fields" value=3>
> <input type=submit>
> </form>';
>
>[color=green]
> >[color=darkred]
> >>$fooboo = "$a" . "$b";[/color]
> >
> >
> > Just do: $fooboo = $a . $b;
> >[/color]
> With just $fooboo = $a . $b; then $fooboo is set to "foobar" not "blah
> blah" as op wanted.
>
> Sacs[/color]

or even funkier ->

for( $i=1; $i <= $num_fields; $i++) {
${blah_$i} = $_REQUEST["field_$i"];
// variable creation on the fly...

// now $blah_1 - $blah_n holds what was input into the appropriate
input
echo "${blah_$i} \n\r";
}

echo '<form>
<input type=text name="field_1">
<input type=text name="field_2">
<input type=text name="field_3">
<input type=hidden name="num_fields" value=3>
<input type=submit>
</form>';



Norm
--
FREE Avatar hosting at www.easyavatar.com


Closed Thread