By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Dave wrote:
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.


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
Jul 17 '05 #2

P: n/a
Sacs wrote:
Variable variables:
http://www.php.net/manual/en/languag...s.variable.php
Yes. But why......:
$fooboo = "$a" . "$b";


Just do: $fooboo = $a . $b;
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3

P: n/a
Ewoud Dronkert wrote:
Sacs wrote:
Variable variables:
http://www.php.net/manual/en/languag...s.variable.php

Yes. But why......:


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>';

$fooboo = "$a" . "$b";

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

With just $fooboo = $a . $b; then $fooboo is set to "foobar" not "blah
blah" as op wanted.

Sacs
Jul 17 '05 #4

P: n/a
Sacs wrote:
$fooboo = "$a" . "$b";


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

With just $fooboo = $a . $b; then $fooboo is set to "foobar" not "blah
blah" as op wanted.


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/
Jul 17 '05 #5

P: n/a
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

Jul 17 '05 #6

P: n/a
Ewoud Dronkert wrote:
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).


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

Jul 17 '05 #7

P: n/a
Philip Olson wrote:
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

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
Jul 17 '05 #8

P: n/a
Ewoud Dronkert wrote:
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).

Oh. Wrong end of stick. :-)

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

Sacs
Jul 17 '05 #9

P: n/a
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:
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
}

Jul 17 '05 #10

P: n/a
On 28 Apr 2005 16:27:31 -0700, BKDotCom wrote:
It seems the PHP community likes to put quotes around variables.
It's baffeling.


Indeed. Like, echo "$aaargh";
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #11

P: n/a
On Fri, 29 Apr 2005 11:40:01 +1200, Sacs wrote:
Oh. Wrong end of stick. :-)


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/
Jul 17 '05 #12

P: n/a
"Sacs" <al**********@way.co.nz> wrote in message
news:3O*******************@news.xtra.co.nz...
Ewoud Dronkert wrote:
Sacs wrote:
Variable variables:
http://www.php.net/manual/en/languag...s.variable.php

Yes. But why......:


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>';

$fooboo = "$a" . "$b";

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

With just $fooboo = $a . $b; then $fooboo is set to "foobar" not "blah
blah" as op wanted.

Sacs


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
Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.