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

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

 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개

찾다찾다 여기서 찾게 되었네요. 정말 감사합니다.
그러고보니 딱 일년전 글이군요 ㅎ
댓글을 작성하시려면 로그인이 필요합니다. 로그인

프로그램

+
제목 글쓴이 날짜 조회
16년 전 조회 2,200
16년 전 조회 2,857
16년 전 조회 1,737
16년 전 조회 3,591
16년 전 조회 4,428
16년 전 조회 3,096
16년 전 조회 2,342
16년 전 조회 2,425
16년 전 조회 2,673
16년 전 조회 2,160
16년 전 조회 5,503
16년 전 조회 3,354
16년 전 조회 1,347
16년 전 조회 1,693
16년 전 조회 5,063
16년 전 조회 2,299
16년 전 조회 3,404
16년 전 조회 2,637
16년 전 조회 1,557
16년 전 조회 5,349
16년 전 조회 2,464
16년 전 조회 5,956
16년 전 조회 1,932
16년 전 조회 4,009
16년 전 조회 3,024
16년 전 조회 2,209
16년 전 조회 2,228
16년 전 조회 4,403
16년 전 조회 3,289
16년 전 조회 2,825
16년 전 조회 2,960
16년 전 조회 2,077
16년 전 조회 1,762
16년 전 조회 1,673
16년 전 조회 1,394
16년 전 조회 1,667
16년 전 조회 1,895
16년 전 조회 1,587
16년 전 조회 4,837
16년 전 조회 3,819
16년 전 조회 1,784
16년 전 조회 1,528
16년 전 조회 2,265
16년 전 조회 4,511
16년 전 조회 3,537
16년 전 조회 2,573
16년 전 조회 4,331
16년 전 조회 3,200
16년 전 조회 1,347
16년 전 조회 1,367
16년 전 조회 2,031
16년 전 조회 1,877
16년 전 조회 2,553
16년 전 조회 2,225
16년 전 조회 1,356
16년 전 조회 4,376
16년 전 조회 1,470
16년 전 조회 1,776
16년 전 조회 2,197
16년 전 조회 3,832
16년 전 조회 2,729
16년 전 조회 1,483
16년 전 조회 4,007
16년 전 조회 1,389
16년 전 조회 1,573
16년 전 조회 1,236
16년 전 조회 1,711
16년 전 조회 1,609
16년 전 조회 1,626
16년 전 조회 1,415
16년 전 조회 2,169
16년 전 조회 1,810
16년 전 조회 1,964
16년 전 조회 2,175
16년 전 조회 1,462
16년 전 조회 2,298
16년 전 조회 2,247
16년 전 조회 4,525
16년 전 조회 1,286
16년 전 조회 1,043
16년 전 조회 2,001
16년 전 조회 1,970
16년 전 조회 1,502
16년 전 조회 2,029
16년 전 조회 2,265
16년 전 조회 1,384
16년 전 조회 1,722
16년 전 조회 1,913
16년 전 조회 1,000
16년 전 조회 1,683
16년 전 조회 1,401
16년 전 조회 1,902
16년 전 조회 1,315
16년 전 조회 2,199
16년 전 조회 1,884
16년 전 조회 5,862
16년 전 조회 1,909
16년 전 조회 3,676
16년 전 조회 1,460
16년 전 조회 1,750
🐛 버그신고