Welcome Guest, Not a member yet? Register   Sign In
Feature request - simple function to get variable name as a string
#1

[eluser]adityamenon[/eluser]
Currently, I'm using a custom helper with this code.

But, I think this a necessary function that codeigniter needs to have built in, and loaded automatically... submitted for the kind consideration of the admins for inclusion in the next iteration of CI.
#2

[eluser]WanWizard[/eluser]
Can you give an example (in code) of where you would like to know the name of a variable, but you don't? I can't imagine needing a function like this...
#3

[eluser]adityamenon[/eluser]
I regret posting this request now. Indeed, Wizard, you are right, there is no situation where this would be needed. I was going to use this for inserting TRUE/FALSE values into a database depending on whether or not a corresponding checkbox was ticked. I thought this would be necessary, but I could achieve the same with a simple foreach $key = $value construct. Sorry for wasting all of your time.

Still though, the code I was about to use was:
Code:
<?php
  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;
  }
?>

It's in the PHP manual.
#4

[eluser]WanWizard[/eluser]
I saw the code, I just struggled to find a practical application, hence the question.




Theme © iAndrew 2016 - Forum software by © MyBB