From: http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php


How to get a variable name as a string in PHP
PHP – Jean-Jacques Guegan
On rare occasions, you may need to retrieve a variable name as a string.

This handy little function retreives the name of the variable:

< ?php
      $my_var = 1;
      echo var_name ($my_var);
?>

will give ‘my_var’.

I started trying to find a solution to this problem, because I needed such a function and because questions about it appeared on several mailing lists and forums:
[PHP] Is there a way to get a variable name as a string?

most people said it was not possible…
Re: [PHP] Is there a way to get a variable name as a string?
From: Rasmus Lerdorf

In PHP it is possible to use a variable if you have its name as a string :

 
< ?php
    $iVarName = 'MaxSize';
    $$iVarName = 10;
    echo $MaxSize;
?>

But PHP does not natively include a way to get the name of a given variable.

This might be useful in situations where you design a function with a parameter passed by reference and you need to know which variable was sent as a parameter for this function.

The solution :
The following function retreives the variable name from a given variable:

< ?php
 function var_name (&$iVar, &$aDefinedVars)
    {
    foreach ($aDefinedVars as $k=>$v)
        $aDefinedVars_0[$k] = $v;
 
    $iVarSave = $iVar;
    $iVar     =!$iVar;
 
    $aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
    $iVar      = $iVarSave;
 
    return $aDiffKeys[0];
    }
 
?>

This function has to be called with a second parameter always set to the result of the function “get_defined_vars()”:

< ?php	
 var_name($iVar, get_defined_vars());
?>

How does it work ?
var_name compares the result of the function “get_defined_vars()” before and after modification of the variable whose name we want to find.
The initial set of defined variables passed as a parameter to var_name is first stored in “$aDefinedVars_0” and compared later.
Before modifying the value of the variable, its value is saved in $iVarSave.

$iVar

is then changed to something different :

$iVar=!$iVar;

$aDefinedVars keeps track of this modification and can be compare to $aDefinedVars_0 (its initial value). The difference is the variable we modified and we can get its name as a string in the key value of the array record.

The value of the variable is restored to its initial value kept in $iVarSave and the name of the variable is returned.
Example :
The following PHP code shows the function var_name in action:

< ?php
$v_1 = 1;                               echo 'var $',var_name ($v_1, get_defined_vars()),' = ',var_dump($v_1),'
'; $v_2 = 4; echo 'var $',var_name ($v_2, get_defined_vars()),' = ',var_dump($v_2),'
'; $v_3 = 'qwerty'; echo 'var $',var_name ($v_3, get_defined_vars()),' = ',var_dump($v_3),'
'; $v_4 = array('aa'=>'11','bb'=>'22',3); echo 'var $',var_name ($v_4, get_defined_vars()),' = ',var_dump($v_4),'
'; $v_5 = &$v_2; echo 'var $',var_name ($v_5, get_defined_vars()),' = ',var_dump($v_5),'
'; echo 'var $',implode (' / $',var_name ($v_5, get_defined_vars(),true)),' = ',var_dump($v_5),'
'; function test() { $v_1 = 'qwerty'; echo 'var $',var_name ($v_1, get_defined_vars()),' = ',var_dump($v_1),'

'; global $v_5; echo 'var $',var_name ($v_5, get_defined_vars()),' = ',var_dump($v_5),'

'; } test(); ?>

var_name can retreive the name of the variable in every case.

Application :
The most direct application of this function is, of course, a “dump” function.
Just call this “dump” function with the variable you want to monitor – do not forget to add the “get_defined_vars()” parameter…

< ?php	
 
function dump(&$v, &$aDefinedVars)
    {
    echo '
',implode (' / $',var_name ($v, $aDefinedVars, true)),' = ',print_r($v, true),'

'; } dump($v_4, get_defined_vars()); ?>

Going further :
The function var_name can be enhanced by adding the parameter $bShowAllRef to show, if set to true, all the references relating to the variable passed as a parameter:

 
< ?php
 
function var_name (&$iVar, &$aDefinedVars, $bShowAllRef=false )
    {
    foreach ($aDefinedVars as $k=>$v)
        $aDefinedVars_0[$k] = $v;
 
    $iVarSave = $iVar;
    $iVar     =!$iVar;
 
    $aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
    $iVar      = $iVarSave;
 
    return ($bShowAllRef? $aDiffKeys: $aDiffKeys[0]);
    }
 
?>

That’s it ! You can download the function here.

This code is free to use and published under the GPL license.

 1,505 total views

Use a CURSOR to loop and ALTER DEFAULT CONSTRAINTs


--declare variables to use in cursor
declare @tablename varchar(1000)
declare @columnname varchar(1000)
declare @defaultvalue varchar(1000)
declare @constraintname varchar(1000)
-- create the cursor
declare _iterativecursor cursor for
select tablename2, columnname2, defaultvalue2, consname2 from temp_default_list1
--open the cursor
open _iterativecursor
--fetch the next record from the cursor
fetch next from _iterativecursor into @tablename, @columnname, @defaultvalue, @constraintname
-- loop through the table cursor reading each row of data and updating table values
while @@fetch_status = 0
begin
--print @tablename2
--print @constraintname2
--print @defaultvalue2
--print @columnname2
-- we can't use alter table in a cursor so we have to hack something together by using the exec statement
EXEC ('ALTER TABLE ' + @tablename + ' ADD CONSTRAINT ' + @constraintname + ' DEFAULT ' + @defaultvalue + ' FOR ' + @Columnname)
fetch next from _iterativecursor into @tablename, @columnname, @defaultvalue, @constraintname
end
--release the cursor
close _iterativecursor
deallocate _iterativecursor

 1,112 total views,  1 views today

Sometimes the best way to find behavior patterns is to plot them out on a graph. Yawnlog helps identify your sleep problems by displaying your snooze data in chart-form.

To get started with the web-based sleep tracking service, sign up for a free account. You can start entering sleep data right away, including backdating entries to include info from nights past. Record the approximate time you fell asleep, the time you woke up, and the quality of your sleep each night; Yawnlog will do the rest. You can even add notes to each entry if you want to keep track of variables like how medication you’re taking might be affecting your slumber.

Yawnlog gives you an overview of your daily and average sleep debt (if any), as well as an average of how much you sleep per night and how close you are to meeting your nightly sleep goals. To see how your numbers compare, Yawnlog lets you connect with other users so you can see how much sleep time you get compared to your friends. You can make your profile and data public so others can find you, or mark it private so it’s accessible only to you.

Yawnlog isn’t as robust as some of the other sleep tracking apps we’ve mentioned, but it’s a great way to get some insight into what affects your sleep and what kinds of overall slumber habits you’re developing.







 948 total views