위

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

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

 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,188건
+
제목 글쓴이 날짜 조회
07-07-16 조회 3,127
07-07-06 조회 3,017
07-07-06 조회 3,448
07-06-23 조회 3,274
07-06-16 조회 2,783
07-05-20 조회 4,297
07-05-09 조회 3,470
07-05-09 조회 6,112
07-05-04 조회 2,707
07-04-30 조회 9,841
07-04-27 조회 4,342
07-04-26 조회 3,460
07-04-25 조회 5,304
07-04-18 조회 7,798
07-04-13 조회 3,135
07-04-12 조회 6,645
07-04-12 조회 5,059
07-04-12 조회 3,368
07-04-11 조회 5,731
07-04-05 조회 6,372
07-03-24 조회 4,429
07-03-20 조회 3,752
07-03-18 조회 3,996
07-03-18 조회 4,745
07-03-17 조회 4,242
07-03-16 조회 4,137
07-03-15 조회 5,306
07-03-13 조회 5,409
07-03-13 조회 3,590
07-03-08 조회 4,510
07-03-08 조회 3,993
07-03-08 조회 3,308
07-03-08 조회 4,193
07-03-05 조회 4,451
07-03-04 조회 2,920
07-03-01 조회 5,956
07-02-28 조회 3,236
07-02-27 조회 4,422
07-02-26 조회 3,336
07-02-26 조회 3,048
07-02-23 조회 3,680
07-02-23 조회 3,646
07-02-22 조회 4,033
07-02-22 조회 3,845
07-02-19 조회 3,859
07-02-17 조회 4,877
07-02-14 조회 8,184
07-02-12 조회 3,952
07-02-10 조회 3,580
07-02-02 조회 3,824
07-01-31 조회 3,497
07-01-29 조회 3,358
07-01-25 조회 5,127
07-01-21 조회 3,274
07-01-19 조회 3,064
06-12-15 조회 4,076
06-12-15 조회 4,476
06-12-15 조회 2,968
06-12-15 조회 4,517
06-12-15 조회 3,403
06-12-15 조회 4,102
06-12-15 조회 3,056
06-12-15 조회 2,780
06-12-15 조회 4,640
06-12-15 조회 3,188
06-12-15 조회 3,290
06-12-07 조회 4,740
06-12-07 조회 4,024
06-12-04 조회 4,675
06-11-27 조회 3,716
06-11-21 조회 4,024
06-11-15 조회 3,456
06-11-04 조회 3,352
06-10-29 조회 4,005
06-10-13 조회 3,872
06-10-13 조회 4,233
06-10-13 조회 4,184
06-10-12 조회 5,379
06-10-12 조회 4,296
06-10-04 조회 3,792
06-10-02 조회 4,569
06-09-29 조회 3,058
06-09-26 조회 3,406
06-09-26 조회 3,663
06-09-26 조회 3,306
06-09-26 조회 3,454
06-09-26 조회 4,364
06-09-25 조회 4,664
06-09-22 조회 5,005
06-09-22 조회 3,570
06-09-21 조회 4,345
06-09-21 조회 4,348
06-09-21 조회 3,553
06-09-21 조회 5,203
06-09-21 조회 3,733
06-09-21 조회 3,461
06-09-20 조회 3,046
06-09-15 조회 3,172
06-09-13 조회 3,486
06-09-11 조회 3,288