위

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

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

 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-04 조회 2,823
10-10-03 조회 2,030
10-10-03 조회 2,541
10-10-02 조회 2,427
10-10-02 조회 2,020
10-10-02 조회 2,047
10-10-02 조회 1,947
10-10-02 조회 1,911
10-10-01 조회 2,131
10-10-01 조회 2,507
10-10-01 조회 3,621
10-10-01 조회 2,161
10-10-01 조회 2,280
10-10-01 조회 2,346
10-09-30 조회 2,305
10-09-30 조회 2,445
10-09-30 조회 2,023
10-09-30 조회 3,582
10-09-30 조회 2,127
10-09-30 조회 3,103
10-09-30 조회 2,096
10-09-30 조회 2,068
10-09-30 조회 2,965
10-09-30 조회 2,414
10-09-30 조회 2,668
10-09-30 조회 2,992
10-09-30 조회 2,649
10-09-30 조회 2,744
10-09-30 조회 2,168
10-09-29 조회 3,620
10-09-29 조회 2,366
10-09-29 조회 2,119
10-09-29 조회 2,518
10-09-29 조회 2,191
10-09-29 조회 3,480
10-09-30 조회 2,410
10-09-29 조회 2,185
10-09-29 조회 2,356
10-09-29 조회 2,320
10-09-29 조회 2,337
10-09-29 조회 2,379
10-09-29 조회 1,879
10-09-29 조회 1,834
10-09-29 조회 1,783
10-09-29 조회 2,457
10-09-29 조회 2,277
10-09-28 조회 2,069
10-09-28 조회 2,092
10-09-28 조회 2,461
10-09-28 조회 4,719
10-09-28 조회 2,607
10-09-28 조회 3,294
10-09-27 조회 2,480
10-09-27 조회 2,363
10-09-27 조회 4,036
10-09-26 조회 3,033
10-09-26 조회 3,917
10-09-26 조회 4,220
10-09-25 조회 2,840
10-09-25 조회 2,465
10-09-25 조회 2,857
10-09-25 조회 2,305
10-09-25 조회 2,600
10-09-25 조회 2,494
10-09-25 조회 2,859
10-09-24 조회 3,197
10-09-24 조회 2,294
10-09-24 조회 2,937
10-09-24 조회 2,643
10-09-24 조회 2,408
10-09-24 조회 2,473
10-09-23 조회 2,755
10-09-22 조회 2,131
10-09-21 조회 2,344
10-09-21 조회 3,985
10-09-21 조회 3,013
10-09-21 조회 2,183
10-09-21 조회 2,206
10-09-20 조회 2,703
10-09-20 조회 2,790
10-09-20 조회 3,157
10-09-19 조회 2,472
10-09-19 조회 2,858
10-09-19 조회 2,151
10-09-18 조회 2,539
10-09-18 조회 2,812
10-09-18 조회 4,193
10-09-18 조회 2,598
10-09-18 조회 2,572
10-09-18 조회 2,373
10-09-18 조회 2,303
10-09-17 조회 2,283
10-09-17 조회 4,971
10-09-17 조회 4,393
10-09-17 조회 3,623
10-09-17 조회 3,997
10-09-17 조회 3,114
10-09-17 조회 2,513
10-09-17 조회 2,640
10-09-17 조회 3,949