1

i've been wondering how this could be done, i've got a function.

  $sky = (a range of number 1 to 10);


function condi($nnn){ 
  if($$nnn <= 1){
  echo 'Fails';
  }
  elseif($$nnn <= 3){
  echo 'Respectable';
  }
  elseif($$nnn <= 5){
  echo 'Decent';
  }
  elseif($$nnn <= 7){
  echo 'Great';
  }
  elseif($$nnn <= 9){
  echo 'Legendary';
  }
  elseif($$nnn = 10){
  echo 'Ultimate';
  }
 }

  condi('sky');

But no matter what is the value of $sky, the output is fails.

So i decided to check what is the value of $$nnn, then i realized that the $$nnn doesn't have a value in it. Any help please?

2
  • Variable variables are horrible. Don't use them. Commented Jul 18, 2011 at 7:24
  • If you really want a variable variable/dynamic variable you could write: ${$nnn} to make it more clear what you're really trying to do. Commented Jul 18, 2011 at 7:33

5 Answers 5

2

Replace $$nnn with $GLOBALS[$nnn].

Whatever variable name $nnn contains - it's not local to the function so you need to acess it via the $GLOBALS array (which is much cleaner than doing something like global $$nnn; and then using $$nnn).

Sign up to request clarification or add additional context in comments.

5 Comments

+1 I upvoted both this and global $$nnn; as they are both valid solutions, but you're right, this is cleaner (as long as the variable is brought to the inner scope, using $GLOBALS[$nnn] everywhere would be an eyesore :-)
@rudi_visser What's the difference between global $$nnn; and aliasing like $$nn = $GLOBALS[$nnn]; How is one cleaner than the other. I've been using global $o; to import global scoped variables (not that I use them much) for a long time :S
Thanks, still pretty new to PHP :) Works well!
@PaulPRO Some people see global as a (excuse me) bastardisation of programming like goto. I'm not exactly one of them, but where possible I do believe it is better to encapsulate functions that rely on external variables in classes of their own, where you can import everything you need in a way that makes sense. What I meant by aliasing the variable is doing something such as: function name($varname) { $var = $GLOBALS[$varname]; switch($var) { /* .. */ } } as it's simply cleaner. Variable-variables can be confusing at best of times.. But I think it's mainly down to personal preference :)
@PaulPro also of course, using GLOBALS allows you to have a variable called $$name (ie. $sky) internally aswell! But still, I think the key point is to avoid globals whereever possible.
2

$sky is in the global scope:

function condi($nnn){ 
  global $$nnn;
  if($$nnn <= 1){
    ...

4 Comments

No, you're making it even worse. He doesn't want a global, he just wants to pass an argument to it's function (otherwise the 'function' would be no use).
@Robert He wants to pass a variable name to the function, and utilise that variable (in global scope) from within the function. It's a silly requirement, but this will resolve his issue.
@Robert de W How so? He is passing a string to a function which tells the function which global variable he wants to use.
@PaulPRO; You're right, he does. I made the above conclusion too soon in answering the question.
0

In order for the function to see $sky (which i'm presuming is the goal), it must be visible in the function's scope. Globals aren't visible by default.

You'll need to either say global $$nnn; inside your function, or change $$nnn to $GLOBALS[$nnn] instead. And note, this will only work for globals -- you won't be able to pass the names of other functions' locals and have this function see them.

Comments

-1

You have two $$ signs in your "nnn" variable. Delete one.

Comments

-1

you should use

<?php
$sky = rand(1, 5);
function condi($nnn){
    global $sky;
    switch ($$nnn) {
        case $$nnn < 1:
            echo "Fails";
            break;
        case $$nnn < 3:
            echo "Respectable";
            break;
        case $$nnn <= 5:
            echo "Decent";
            break;
        // .................... //
    }
}
condi('sky');
?>

1 Comment

global $sky; defeats the purpose of a variable variable if you have to know what it's going to contain in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.