위

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

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

 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건
+
제목 글쓴이 날짜 조회
10-09-25 조회 2,761
10-09-19 조회 2,796
10-09-15 조회 2,810
10-09-15 조회 4,218
10-09-15 조회 3,450
10-09-15 조회 2,544
10-09-07 조회 3,662
10-08-27 조회 4,120
10-06-11 조회 4,299
10-05-31 조회 2,546
10-02-27 조회 5,058
10-02-09 조회 3,804
09-10-10 조회 7,398
09-03-19 조회 3,253
09-02-09 조회 3,524
09-02-09 조회 4,569
08-11-01 조회 2,955
08-09-29 조회 4,608
08-09-26 조회 3,540
08-09-26 조회 3,614
08-09-26 조회 2,751
08-09-26 조회 3,794
08-09-26 조회 3,523
08-09-26 조회 4,294
08-09-18 조회 3,638
08-09-09 조회 3,268
08-08-28 조회 2,815
08-07-06 조회 3,580
08-05-21 조회 4,741
08-05-13 조회 4,587
08-04-17 조회 4,955
08-02-19 조회 3,572
08-01-18 조회 3,905
08-01-10 조회 3,867
07-10-22 조회 3,699
07-10-09 조회 4,365
07-10-03 조회 3,574
07-07-06 조회 4,247
07-07-06 조회 4,461
07-07-06 조회 4,080
07-06-28 조회 3,605
07-06-05 조회 3,619
07-06-05 조회 4,173
07-05-30 조회 3,333
07-05-02 조회 3,714
07-04-15 조회 5,201
07-04-11 조회 3,678
07-04-05 조회 3,980
07-03-31 조회 3,187
07-03-31 조회 4,160
07-03-30 조회 3,780
07-03-24 조회 4,034
07-03-22 조회 3,697
07-03-12 조회 4,451
07-02-16 조회 3,322
07-02-14 조회 5,201
07-02-10 조회 4,046
07-02-09 조회 4,430
07-01-19 조회 4,092
07-01-16 조회 5,026
06-12-16 조회 5,890
06-11-30 조회 3,648
06-11-25 조회 3,762
06-10-15 조회 5,113
06-09-26 조회 5,344
06-09-26 조회 5,468
06-09-22 조회 4,331
06-09-21 조회 3,572
06-09-21 조회 3,675
06-09-02 조회 3,519
06-08-08 조회 5,156
06-07-20 조회 3,782
06-07-17 조회 3,403
06-07-13 조회 4,231
06-07-11 조회 3,520
06-07-11 조회 3,523
06-07-11 조회 3,881
06-06-29 조회 6,124
06-06-19 조회 6,498
06-06-02 조회 3,562
06-05-24 조회 5,032
06-05-14 조회 7,831
06-05-08 조회 3,941
06-02-02 조회 5,498
05-12-16 조회 4,569
05-12-05 조회 5,202
05-06-28 조회 4,166
05-04-11 조회 4,116
05-04-11 조회 4,295
05-04-11 조회 3,355
05-03-13 조회 3,268
05-03-12 조회 4,328
04-12-08 조회 7,821
04-12-01 조회 5,226
04-11-28 조회 3,419
04-11-28 조회 3,249
04-08-05 조회 5,668
04-08-05 조회 5,013
04-08-05 조회 5,626