473,396 Members | 2,139 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.

Easy question, return variable name

Tom
Is there a function to get a variable to return its name w/datatype of
string? i.e.:

$var would return "var"

Dec 14 '06 #1
8 1692
string gettype ( mixed var )

this function will return the variable type in string format.

Thanks

Tom wrote:
Is there a function to get a variable to return its name w/datatype of
string? i.e.:

$var would return "var"
Dec 14 '06 #2
Tom
Sorry, I should have been more specific -- I need the actual name of
the variable itself, not its datatype. So, I need function that does
the following:

<?php

$myArray = array( );
$myVariable = "foobar";

echo some_function($myArray); // should echo "myArray"
echo some_function($myVariable); // should echo "myVariable"

?>

Josh wrote:
string gettype ( mixed var )

this function will return the variable type in string format.

Thanks

Tom wrote:
Is there a function to get a variable to return its name w/datatype of
string? i.e.:

$var would return "var"
Dec 14 '06 #3
Tom wrote:
Is there a function to get a variable to return its name w/datatype of
string? i.e.:

$var would return "var"
No there is no such function.

Usually the function will get the value of the given Variable in an
Activation Record. The value carries no information about its source.

If an value is passed as an reference there may be a lot of variables
referring to the same value. Which variables name would you like? When
your code is compiled the result may not carry any names for variables.

Generally there is no good reason why you should need such an function.
What would you like to do?

Heiko
--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage
Dec 14 '06 #4
NC
Tom wrote:
>
Is there a function to get a variable to return its name
No, but you can get a list of all defined variables by calling
get_defined_vars():

http://www.php.net/manual/en/functio...fined-vars.php

Cheers,
NC

Dec 14 '06 #5
On 14 Dec 2006 13:17:01 -0800, "Tom" <bi****@gmail.comwrote:
>Is there a function to get a variable to return its name w/datatype of
string? i.e.:

$var would return "var"

http://groups.google.co.uk/groups/se...o+the+Clown%22
(then run away from it, as it's not a sensible thing to do)

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 14 '06 #6
Rik
Andy Hassall wrote:
On 14 Dec 2006 13:17:01 -0800, "Tom" <bi****@gmail.comwrote:
>Is there a function to get a variable to return its name w/datatype
of
string? i.e.:

$var would return "var"


http://groups.google.co.uk/groups/se...o+the+Clown%22
(then run away from it, as it's not a sensible thing to do)
Hehehe, the horror! Very creative though.
--
Rik Wasmus
Dec 14 '06 #7
Tom wrote:
Sorry, I should have been more specific -- I need the actual name of
the variable itself, not its datatype. So, I need function that does
the following:

<?php

$myArray = array( );
$myVariable = "foobar";

echo some_function($myArray); // should echo "myArray"
echo some_function($myVariable); // should echo "myVariable"

?>
No can do. Please have a in-depth look at the PHP manual,
chapter "References to variables".

I'll try to show you the inability to know the *name* of a variable. And,
please, before replying, do RTFM.

<?php

$a = "foobar";
$b =& $a;
$c =& $a;
$d =& $a;

unset($a);

echo some_function($c); // WTF??

?>

Now, in your opinion, what should this return?? $b, $c and $d all point to
the same memory address, because of how references work. And the name $a,
which originated that memory address, does not exist. So, do we
return 'b', 'c' or 'd'??

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Razón #10 : Niegas vivamente la existencia de miles de dioses adorados por
otras religiones, pero te invade la ira cuando alguien niega la existencia
del tuyo.
-- Una de las jocosas, pero no por eso menos ciertas, Diez señales de
que eres un fundamentalista
Dec 15 '06 #8
Tom
Here's why I needed this "bobo" function:

I'm currently upgrading a website to use DB_DataObjects instead of the
DB class. However, it's a process that will take almost a month
because of the large number of scripts needing the update. In
addition, I need to keep legacy support for the DB class while I'm
upgrading, without creating any additional unnecessary database
connections.

I don't know the actual answer to this, but I always assumed creating
an extension of the DB_DataObject (i.e. class DataObject_MyTable
extends DB_DataObject) would create a separate connection. To get
around this, I was using a rather ugly hack to use the existing
connection from the legacy DB object:

<?php
$DB = DB::connect($dsn, $options);
$_DB_DATAOBJECT['CONNECTIONS']['DB'] =& $DB;
?>

That $_DB_DATAOBJECT array is set in the PEAR class definition in
DB/DataObject.php thru $GLOBALS, and it stores the name of the DB
object as a key. I was trying to write a function that would create
new DB_DataObjects for all existing DB classes without having to define
each one manually.

I've since given up though... I rather enjoyed the bobo_the_clown
function though -- very clever!

Iván Sánchez Ortega wrote:
Tom wrote:
Sorry, I should have been more specific -- I need the actual name of
the variable itself, not its datatype. So, I need function that does
the following:

<?php

$myArray = array( );
$myVariable = "foobar";

echo some_function($myArray); // should echo "myArray"
echo some_function($myVariable); // should echo "myVariable"

?>

No can do. Please have a in-depth look at the PHP manual,
chapter "References to variables".

I'll try to show you the inability to know the *name* of a variable. And,
please, before replying, do RTFM.

<?php

$a = "foobar";
$b =& $a;
$c =& $a;
$d =& $a;

unset($a);

echo some_function($c); // WTF??

?>

Now, in your opinion, what should this return?? $b, $c and $d all point to
the same memory address, because of how references work. And the name $a,
which originated that memory address, does not exist. So, do we
return 'b', 'c' or 'd'??

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Razón #10 : Niegas vivamente la existencia de miles de dioses adorados por
otras religiones, pero te invade la ira cuando alguien niega la existencia
del tuyo.
-- Una de las jocosas, pero no por eso menos ciertas, Diez señales de
que eres un fundamentalista
Dec 15 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Calan | last post by:
Mike, Your code on the dynamic input checking was excellent and very well explained. (The only thing I had to do was change the test for text input to be "1 > len of text", instead or "0 >...
3
by: Larry L | last post by:
This is a novice question, but I'm stumped. I have a variable with a bunch of text assigned to it. Now I want to email the value of the variable. I have a simple form that asks for their name and...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
2
by: Ammar | last post by:
Hi a sily question! I have decleared a string variable and then defined it by adding togather other varibla lide this: mailbody = " Name:" & Box1.Text & "Adress:" & Box2.Text & "Tel:" &...
5
by: LedZep | last post by:
What up, All I need to do is enter a last name in a text box, query a MSAccess database and display the name with the corresponding columns. This is no problem, but when there are more than one...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
3
by: cr113 | last post by:
I'm switching over from VB and I have a question. Suppose I had a VB project with 2 forms and one variable. Call them Form1,Form2 and strUsername. Suppose I want to initialize strUsername to...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.