473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Jul 17 '05 #1
12 51132
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
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
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
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
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David | last post by:
Can anyone give me some help or tips in converting this code to take 2 variables that will specify the number of Pack type lines and the number of Single type lines. We use it to create a web page...
3
by: Marc | last post by:
Hi all, I can't remember how to do this. I have several instances of telnet connections that I label conn2,conn3, etc. Later when I want to scroll through all of these I wanted to do...
4
by: s99999999s2003 | last post by:
hi the database "execute" function returns a list of logical results. Each logical result is a list of row tuples, as explained in the documents. everytime i use it to execute various...
2
by: Jerry Spence1 | last post by:
Sorry for the confusing title - not quite sure what I'm after here. I think need to have a static variable in a procedure, but the name of that variable will be designated at run time. Also I...
3
by: Imran Aziz | last post by:
Hello All, I have a dataset that I populate using a SQL Server database, and the second one that I populate using a mySQL Server database, I need to combine the results in memory and then sort...
1
by: iLL | last post by:
Is there any way to save the result of a "test" command in a variable, or would I have to do something like: bool=`if ; then echo 1; else echo 0; fi;`
10
sumaiya
by: sumaiya | last post by:
The problem is explained below: 1- on a php page i have $x=2; 2- there is a mysql table with a field called "layout" 3- The data in "layout" field = My problem number is $x 4- Now what i want...
3
by: Vik Rubenfeld | last post by:
I have to search 2 mySQL tables, and show the user a single sorted list that contains all the results from both mySQL queries. My question is, how do you get all the resulting items from both...
8
by: vineetbindal | last post by:
Hi All. I have two Columns column1 and column2. i have to run a query with some value from colum1 depending on it will select result from coloumn2 and if that result is present in coloumn 1 it...
3
by: perhapscwk | last post by:
I have $ad_url = "https://".$_SERVER."/category.php?viewadv=".$viewadv; echo $ad_url; It always show as
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.