위

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

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

 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-11-17 조회 2,336
10-11-16 조회 2,646
10-11-16 조회 2,149
10-11-16 조회 2,381
10-11-15 조회 2,856
10-11-15 조회 2,367
10-11-15 조회 3,112
10-11-15 조회 2,133
10-11-15 조회 2,243
10-11-15 조회 2,789
10-11-15 조회 2,490
10-11-14 조회 2,150
10-11-14 조회 4,356
10-11-14 조회 2,685
10-11-14 조회 3,699
10-11-13 조회 3,520
10-11-13 조회 2,501
10-11-13 조회 3,724
10-11-12 조회 2,328
10-11-12 조회 2,714
10-11-11 조회 3,890
10-11-11 조회 2,103
10-11-11 조회 2,721
10-11-10 조회 2,947
10-11-10 조회 2,217
10-11-10 조회 2,568
10-11-10 조회 2,050
10-11-09 조회 3,382
10-11-09 조회 2,896
10-11-09 조회 2,229
10-11-09 조회 1,930
10-11-09 조회 1,997
10-11-09 조회 2,541
10-11-09 조회 2,502
10-11-08 조회 1,910
10-11-08 조회 1,797
10-11-08 조회 2,086
10-11-08 조회 2,089
10-11-08 조회 3,011
10-11-08 조회 1,930
10-11-08 조회 2,074
10-11-08 조회 2,036
10-11-08 조회 2,290
10-11-08 조회 3,061
10-11-07 조회 2,950
10-11-07 조회 1.6만
10-11-07 조회 4,530
10-11-06 조회 2,085
10-11-06 조회 2,095
10-11-06 조회 2,247
10-11-06 조회 2,025
10-11-06 조회 1,988
10-11-06 조회 1.3만
10-11-05 조회 2,575
10-11-05 조회 2,551
10-11-05 조회 2,683
10-11-04 조회 2,590
10-11-04 조회 1,940
10-11-04 조회 2,795
10-11-04 조회 2,324
10-11-04 조회 1,966
10-11-04 조회 2,144
10-11-04 조회 2,204
10-11-04 조회 2,782
10-11-04 조회 2,131
10-11-04 조회 2,108
10-11-04 조회 2,337
10-11-03 조회 2,115
10-11-03 조회 2,972
10-11-03 조회 2,692
10-11-03 조회 4,272
10-11-03 조회 2,973
10-11-03 조회 2,002
10-11-03 조회 1,951
10-11-02 조회 2,175
10-11-02 조회 2,787
10-11-02 조회 2,810
10-11-01 조회 2,017
10-11-01 조회 2,107
10-11-01 조회 2,391
10-11-01 조회 2,605
10-10-31 조회 2,369
10-10-31 조회 2,323
10-10-31 조회 2,589
10-10-30 조회 2,000
10-10-30 조회 1,977
10-10-30 조회 2,499
10-10-30 조회 1,998
10-10-29 조회 3,166
10-10-29 조회 3,749
10-10-29 조회 2,183
10-10-29 조회 2,261
10-10-29 조회 3,137
10-10-28 조회 2,365
10-10-28 조회 2,460
10-10-28 조회 2,858
10-10-28 조회 3,014
10-10-28 조회 3,273
10-10-27 조회 2,575
10-10-27 조회 3,324