위

넘겨온 변수의 이름을 추출하는 함수..

값이아니라 변수명입니다~

 Here s a function to get the name of a given variable.  Explanation and examples below.

   function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
   {
     if($scope) $vals = $scope;
     else      $vals = $GLOBALS;
     $old = $var;
     $var = $new = $prefix.rand().$suffix;
     $vname = FALSE;
     foreach($vals as $key => $val) {
       if($val === $new) $vname = $key;
     }
     $var = $old;
     return $vname;
   }


 Explanation:

 The problem with figuring out what value is what key in that variables scope is that several variables might have the same value.  To remedy this, the variable is passed by reference and its value is then modified to a random value to make sure there will be a unique match.  Then we loop through the scope the variable is contained in and when there is a match of our modified value, we can grab the correct key.

 Examples:

 1.  Use of a variable contained in the global scope (default):
 < ?php
   $my_global_variable = "My global string.";
   echo vname($my_global_variable); // Outputs:  my_global_variable
 ? >

 2.  Use of a local variable:
 < ?php
   function my_local_func()
   {
     $my_local_variable = "My local string.";
     return vname($my_local_variable, get_defined_vars());
   }
   echo my_local_func(); // Outputs: my_local_variable
 ? >

 3.  Use of an object property:
 < ?php
   class myclass
   {
     public function __constructor()
     {
       $this->my_object_property = "My object property  string.";
     }
   }
   $obj = new myclass;
   echo vname($obj->my_object_property, $obj); // Outputs: my_object_property
 ? >
 =>2줄로
 < ?php
 function var_name(&$var, $scope=0)
 {
     $old = $var;
     if (($key = array_search($var = 'unique'.rand().'value', !$scope ? $GLOBALS : $scope)) && $var = $old) return $key;
 }
 ? >

[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]

댓글 1개

찾다찾다 여기서 찾게 되었네요. 정말 감사합니다.
그러고보니 딱 일년전 글이군요 ㅎ
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
10-09-17 조회 2,350
10-09-16 조회 3,435
10-09-16 조회 2,158
10-09-16 조회 3,291
10-09-16 조회 2,235
10-09-16 조회 3,920
10-09-16 조회 2,173
10-09-16 조회 2,661
10-09-16 조회 2,177
10-09-15 조회 4,509
10-09-15 조회 2,864
10-09-15 조회 2,854
10-09-15 조회 2,859
10-09-15 조회 2,889
10-09-15 조회 2,640
10-09-15 조회 3,349
10-09-15 조회 2,847
10-09-15 조회 2,423
10-09-15 조회 4,282
10-09-15 조회 2,699
10-09-15 조회 2,913
10-09-15 조회 2,614
10-09-15 조회 3,516
10-09-15 조회 3,530
10-09-15 조회 3,188
10-09-15 조회 2,541
10-09-15 조회 2,744
10-09-15 조회 2,407
10-09-15 조회 2,419
10-09-15 조회 2,610
10-09-14 조회 4,133
10-09-11 조회 4,071
10-09-11 조회 3,000
10-09-11 조회 2,515
10-09-11 조회 2,706
10-09-11 조회 2,691
10-09-11 조회 2,572
10-09-11 조회 2,486
10-09-11 조회 2,387
10-09-11 조회 2,733
10-09-11 조회 2,831
10-09-11 조회 2,462
10-09-11 조회 2,461
10-09-11 조회 2,642
10-09-11 조회 3,112
10-09-11 조회 2,621
10-09-08 조회 3,002
10-09-08 조회 2,735
10-09-07 조회 3,511
10-09-07 조회 4,573
10-09-07 조회 3,896
10-09-07 조회 2,449
10-09-07 조회 3,432
10-09-07 조회 3,723
10-09-04 조회 2,989
10-08-29 조회 2,877
10-08-27 조회 4,178
10-08-27 조회 4,185
10-08-20 조회 7,059
10-08-06 조회 2,974
10-07-30 조회 5,736
10-07-27 조회 2,839
10-07-15 조회 3,179
10-07-06 조회 3,252
10-07-01 조회 3,132
10-07-01 조회 2,655
10-07-01 조회 2,889
10-07-01 조회 3,146
10-06-30 조회 3,034
10-06-30 조회 3,905
10-06-30 조회 2,900
10-06-30 조회 3,008
10-06-30 조회 2,467
10-06-30 조회 2,593
10-06-30 조회 2,611
10-06-30 조회 2,539
10-06-30 조회 2,538
10-06-30 조회 2,564
10-06-30 조회 2,705
10-06-30 조회 2,724
10-06-30 조회 4,134
10-06-30 조회 2,478
10-06-30 조회 3,158
10-06-30 조회 3,063
10-06-17 조회 3,165
10-06-16 조회 2,861
10-06-14 조회 3,430
10-06-13 조회 2,122
10-06-11 조회 2,775
10-06-11 조회 2,677
10-06-11 조회 4,386
10-06-11 조회 4,771
10-06-11 조회 3,458
10-06-01 조회 2,955
10-05-31 조회 3,657
10-05-31 조회 3,076
10-05-31 조회 2,638
10-05-22 조회 5,311
10-05-18 조회 2,996
10-05-18 조회 3,025