위

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

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

 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-10-27 조회 2,973
10-10-27 조회 2,209
10-10-26 조회 3,316
10-10-26 조회 2,310
10-10-26 조회 2,205
10-10-26 조회 2,213
10-10-25 조회 2,137
10-10-24 조회 2,323
10-10-24 조회 2,364
10-10-24 조회 2,656
10-10-24 조회 4,819
10-10-24 조회 3,338
10-10-23 조회 2,281
10-10-23 조회 3,096
10-10-22 조회 2,127
10-10-22 조회 2,281
10-10-22 조회 3,457
10-10-22 조회 3,173
10-10-22 조회 2,198
10-10-21 조회 2,510
10-10-21 조회 2,462
10-10-21 조회 2,503
10-10-21 조회 2,646
10-10-21 조회 2,394
10-10-21 조회 2,393
10-10-21 조회 2,294
10-10-21 조회 2,471
10-10-21 조회 2,403
10-10-21 조회 3,029
10-10-21 조회 2,182
10-10-20 조회 2,158
10-10-20 조회 2,878
10-10-19 조회 2,891
10-10-19 조회 2,732
10-10-19 조회 2,280
10-10-19 조회 3,017
10-10-19 조회 2,425
10-10-19 조회 3,054
10-10-19 조회 3,471
10-10-19 조회 3,039
10-10-19 조회 9,096
10-10-19 조회 2,615
10-10-19 조회 2,766
10-10-19 조회 2,548
10-10-18 조회 2,649
10-10-18 조회 2,565
10-10-18 조회 2,629
10-10-18 조회 2,315
10-10-18 조회 3,457
10-10-17 조회 2,448
10-10-17 조회 2,310
10-10-17 조회 3,023
10-10-17 조회 2,797
10-10-16 조회 2,459
10-10-16 조회 2,362
10-10-16 조회 2,465
10-10-16 조회 2,836
10-10-16 조회 2,214
10-10-15 조회 2,325
10-10-14 조회 2,494
10-10-14 조회 3,304
10-10-13 조회 2,478
10-10-13 조회 2,622
10-10-13 조회 2,338
10-10-13 조회 2,360
10-10-12 조회 2,783
10-10-12 조회 3,094
10-10-12 조회 2,356
10-10-12 조회 2,830
10-10-11 조회 3,000
10-10-09 조회 2,436
10-10-09 조회 2,300
10-10-07 조회 2,627
10-10-07 조회 2,576
10-10-07 조회 2,276
10-10-07 조회 2,596
10-10-07 조회 2,713
10-10-07 조회 2,735
10-10-06 조회 4,681
10-10-06 조회 2,155
10-10-06 조회 2,914
10-10-06 조회 2,269
10-10-06 조회 3,100
10-10-06 조회 2,094
10-10-06 조회 2,346
10-10-05 조회 2,250
10-10-05 조회 2,299
10-10-05 조회 3,201
10-10-05 조회 4,523
10-10-05 조회 3,453
10-10-05 조회 3,417
10-10-05 조회 2,260
10-10-05 조회 2,475
10-10-05 조회 2,325
10-10-04 조회 2,112
10-10-04 조회 2,497
10-10-04 조회 2,208
10-10-04 조회 2,728
10-10-04 조회 1,991
10-10-04 조회 1,895