Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:54 PM
Dave
Guest
 
Posts: n/a
Default how to combine the results of two variables to echo another variable?

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.


  #2  
Old July 17th, 2005, 12:54 PM
Sacs
Guest
 
Posts: n/a
Default 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
  #3  
Old July 17th, 2005, 12:54 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default 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/
  #4  
Old July 17th, 2005, 12:54 PM
Sacs
Guest
 
Posts: n/a
Default 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
  #5  
Old July 17th, 2005, 12:54 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default 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/
  #6  
Old July 17th, 2005, 12:54 PM
Philip Olson
Guest
 
Posts: n/a
Default 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

  #7  
Old July 17th, 2005, 12:54 PM
BKDotCom
Guest
 
Posts: n/a
Default 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.

  #8  
Old July 17th, 2005, 12:54 PM
Sacs
Guest
 
Posts: n/a
Default 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
  #9  
Old July 17th, 2005, 12:54 PM
Sacs
Guest
 
Posts: n/a
Default 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
  #10  
Old July 17th, 2005, 12:54 PM
Hannes Dorbath
Guest
 
Posts: n/a
Default 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]
  #11  
Old July 17th, 2005, 12:54 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default 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/
  #12  
Old July 17th, 2005, 12:54 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default 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/
  #13  
Old July 17th, 2005, 12:55 PM
Norman Peelman
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.