Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
-
will83
- Forum Commoner
- Posts: 53
- Joined: Thu Nov 10, 2005 3:13 pm
Post
by will83 »
Hi as above,
I'm stuck with using a variable as a query result array field name:
Code: Select all
while ($row = mysql_fetch_array($result)) {
echo "$row['".$cat."']";
}
Whats the right way of doing this cos it's not working!!
I think you have to use the { } somewhere don't you??
Thanks!!
Will
-
sheila
- Forum Commoner
- Posts: 98
- Joined: Mon Sep 05, 2005 9:52 pm
- Location: Texas
Post
by sheila »
-
will83
- Forum Commoner
- Posts: 53
- Joined: Thu Nov 10, 2005 3:13 pm
Post
by will83 »
god im such a dumb ass at times.
Thanks for that

-
josh
- DevNet Master
- Posts: 4872
- Joined: Wed Feb 11, 2004 3:23 pm
- Location: Palm beach, Florida
Post
by josh »
Its called expanding your variables
the {} forces variables to expand, but only in double quotes
outputs the value of $test
"$test"
outputs the value of $testing
"$testing"
outputs the value of test, then the string "ing"
"{$test}ing"
-
Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Post
by Chris Corbyn »
jshpro2 wrote:Its called expanding your variables
the {} forces variables to expand, but only in double quotes
outputs the value of $test
"$test"
outputs the value of $testing
"$testing"
outputs the value of test, then the string "ing"
"{$test}ing"
For added interest on the { } syntax in variables/functions.
Code: Select all
$foo123 = 'Hello World!';
$bar = 'foo';
echo ${$bar.'123'}; //Hello World!
///////////////////////////////////////
function foo123()
{
//do something
}
$something = {$bar.'123'}();
Never really comes in useful but I've found myself using the whacky function stuff before in some more abstract OOP stuff.
-
Jenk
- DevNet Master
- Posts: 3587
- Joined: Mon Sep 19, 2005 6:24 am
- Location: London
Post
by Jenk »
If you ever need to create a callback in one of your methods/functions then {} become a necessity.
Code: Select all
<?php
function myCallBack ($func, $param)
{
return {$func}($param);
}
?>
-
Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Post
by Chris Corbyn »
Jenk wrote:If you ever need to create a callback in one of your methods/functions then {} become a necessity.
Code: Select all
<?php
function myCallBack ($func, $param)
{
return {$func}($param);
}
?>
True

It will still work without the { } for single strings where there's no string manipulation such as concatentaion or case-chaning going on...
Code: Select all
function foo()
{
echo 'Function was called';
}
$var = 'foo';
$var();