How to echo a variable name?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ganymed
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 3:32 am
Location: Austria, Vienna

How to echo a variable name?

Post by Ganymed »

I would like to echo the name of variable - not its content. Is this possible?


Detailed example:

$db_error = "Sorry, there was an error: ".$sql_statement;

echo $db_error; would print something like:

"Sorry, there was an error: INSERT INTO table ..."

but i am searching for a way to echo:

"Sorry, there was an error: sql_statement"


I am searching for an answer quiet a while but neither did i find a solution nor a "thats not possible".
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Someone wrote a function like that at TheScripts.

But... I still don't see any time that you'd need to do this and somehow not already know the variable's name.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$foo = 'bar';

echo $$foo; //outputs foo
I'm not sure if this was what you wanted, as per your example.
Ganymed
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 3:32 am
Location: Austria, Vienna

Post by Ganymed »

Thanks you for your replies.

@ Jcart

foo = 'bar';
echo $$foo;

That doesn't echo anything (at least on my side - php5)


@ superdezign

That does the trick, thx very much.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Ganymed wrote:

Code: Select all

foo = 'bar'; 
echo $$foo;
That doesn't echo anything (at least on my side - php5)
Hope this will help you understand the logic

Code: Select all

$bar = 'Hello';
$foo = 'bar'; 
echo $$foo;
Ganymed
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 3:32 am
Location: Austria, Vienna

Post by Ganymed »

Gente wrote:Hope this will help you understand the logic
Ok ... now I get it - but thats not what I wanted.

Sadly after some reflection superdezign tip doesn't help also.

To be more specific:

I make a query and if it goes bad I pass the $sql_statement to a function that does a nice formating and the like. Something like that:


function nice_error_output ($error) {
echo "Sorry, something got wrong with ". $error;
}

$sql_user_quer = "SELECT user_id FROM user ...";

pg_query ($sql_user_query, $con) or die ( nice_error_output($sql_user_query) );


I am searching for a "switch" in the nice_error_output function so that in productive enviroment the output is:
Sorry, something got wrong with sql_user_query"

And in Testing enviroment the output is:
Sorry, something got wrong with SELECT user_id FROM user ..."

So in in answer to superdezign question why ... in this case I dont know which sql_string variable I am passing to the function. Could be $sql_user_query or $sql_user_delete_query or whatever.
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Post by idevlin »

Why not pass a flag to nice_error_output to indicate what type of error it is? I'm sure there are only a limited number of error types that you wish to pass, and you could then switch on that?

e.g. (where $type is an integer, e.g. 1 - SELECT, 2 - DELETE..)

Code: Select all

function nice_error_output ($type,$error) {
   $error_type="";

   switch ($type) {
      case SELECT:
          $error_type="sql_user_query";
          break;
      case DELETE:
         $error_type="sql_delete_query";
         break;
   }

   echo "Sorry, something went wrong with " . $error_type . ": error - " . $error;
}
Or something like that?

Alternatively you could have an array that maps the integer error_type to the string error type.

Just a thought.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Re: How to echo a variable name?

Post by Z3RO21 »

Code: Select all

$db_error = 'Sorry, there was an error: sql_statement'
If you want to use a static string like that use a static string :roll:
Ganymed
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 3:32 am
Location: Austria, Vienna

Post by Ganymed »

I found a simple solution for my problem. I'm going to pass two strings to the nice_error_output function and echo one of them depending on the system (live or testing).

Thanks to all for your answers and suggestions, I could solve my problem and learned a lot.

So long,

Ganymed
Post Reply