0

I have a series of variables in php:

move1A = $row['column'];
move2A = $row['column'];
move3A = $row['column'];
etc.

I want to create a variable that will represent these variables and check if it is NULL. I have spent quite a bit of time looking, and although I believe that variable variables may be the key to my problem. So far I have tried:

$current_move_check = 'move'.$moveCounter.'A';

and

$current_move_check = '$move'.$moveCounter.'A';

Neither seem to work. Any suggestions for making something like this work? Thanks!

UPDATE:

So I'm trying to loop the moveXA variables based on user input and I need to run different script whether it is null or set.

I thought that:

elseif (!$$current_move_check) {

and

elseif ($$current_move_check) {

would work but they seem to not be outputting as expected.

5
  • I have a feeling there's a better way to achieve whatever you're trying to achieve. Can you post what you actually are trying to do? Commented Aug 13, 2014 at 9:02
  • Did you try $moveName = 'move'.$moveCounter.'A'; $current_move_check = $$moveName? But I'm with @Prisoner, if you explain more you can try to help you in other possible directions... Commented Aug 13, 2014 at 9:03
  • Why don't you use an array ? $moveA[1] = ...; $current_move = $moveA[$moveCounter]; It would makes more sense in your case Commented Aug 13, 2014 at 9:03
  • Variable variables are never a good idea. Unless you know when it makes sense to break that rule, don't. Use arrays instead. You'll have to describe the problem you're trying to solve (and not the problem with how you're trying to solve it) to get a proper solution. Commented Aug 13, 2014 at 9:04
  • Alrighty. I'll get it to work with arrays. Thanks for the advice. For anyone who is looking for a similar answer, I did get it to work with if(empty($$current)) and if(!empty($$current)). Commented Aug 13, 2014 at 9:28

2 Answers 2

1

Considering your update, I'd really suggest you to use an array, rather than the variable variables trick. Your code would makes more sense and be easier to maintain :

$count = 0;
$moveA[++$count] = $row['column'];
$moveA[++$count] = $row[...];
$moveA[++$count] = $row[...];
...

foreach ($moveA as $key => $value) {
    if ($value) { // = $$current_move_check 

    } else { // = !$$current_move_check

    }
}

As @MatsLindh pointed out in its comment : "Variable variables are never a good idea. Unless you know when it makes sense to break that rule, don't."

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

Comments

0
$current_move_check = 'move'.$moveCounter.'A';
echo $$current_move_check;

now you can check this as well like

if($$current_move_check!=NULL)
{
     // do your code
} 

Comments

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.