473,796 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1712
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_var s():

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.c omwrote:
>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.u k :: 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.c omwrote:
>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_MyTa ble
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($ds n, $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
1392
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 > length of text", and add a line to store the result for use on the server). Thank you very much! I have a related issue that you might have some ideas on. In some cases, I have a select box where one of the selections may require an additional input...
3
1855
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 emails that, but how do I add the variable result? Thanks much, Larry
21
4083
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 A { std::vector<B*> Bs; public:
2
1106
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:" & Box3.Text........ I wanted in fact to use this variable as a body of an e mail to be sent to me, and i wanted the result to be something like this:
5
1533
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 records with the same last name, I need to click a command button to display the next record with that name. I cant find it in any of my books and it sounds like an easy enough question. Any help is definately appreciated. TIA,
20
7013
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
3
2973
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 "jones" and use this value in Form1 and Form2 at any time. In VB I would create a module with the following code: Module test Public strUsername As String
20
4045
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 tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
12
3020
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
0
9533
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.