Page 1 of 1
How to echo a variable name?
Posted: Fri Jun 29, 2007 3:49 am
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".
Posted: Fri Jun 29, 2007 4:23 am
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.
Posted: Fri Jun 29, 2007 5:08 am
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.
Posted: Fri Jun 29, 2007 6:38 am
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.
Posted: Fri Jun 29, 2007 6:42 am
by Gente
Ganymed wrote:
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;
Posted: Fri Jun 29, 2007 7:08 am
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.
Posted: Fri Jun 29, 2007 7:40 am
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.
Re: How to echo a variable name?
Posted: Fri Jun 29, 2007 9:15 am
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

Posted: Mon Jul 02, 2007 3:41 am
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