1

I have some variables like this:

$barbarian = 7;
$archer = 7;
$giant = 7;

And a function like this:

function army($troopData,$troop,$thing){

    if(isset($troop) && $troopData+1 == ${"$thing"}){
        $troopData++;
        $text = "<span class='army-icon active top-lvl $thing-icon'><span>$troopData</span></span>";
    }elseif(isset($troop)){
        $troopData++;
        $text = "<span class='army-icon active $thing-icon'><span>$troopData</span></span>";
    }else{
        $text = "<span class='army-icon $thing-icon'><span></span></span>";
    }
  return $text;

}

Notice the first if: if(isset($troop) && $troopData+1 == ${"$thing"}){

The function is called like this: army($troopslvl4000000,$troops4000000, "barbarian"); army($troopslvl4000001,$troops4000001, "archer");

The last parameter is named $thing in the function.

So I need to first line of the if to check if:

1) $troop is set. 2) $troopData+1 == $barbarian (or whatever thing is. $archer for the second function call)

But it isn't working. It is always using the elseif or the else. The first if never gets called...

5
  • In your first if statement, what is this ${"$thing"} supposed to be? That doesn't look like valid PHP to me Commented Aug 21, 2015 at 14:41
  • @mituw16 that is variable variablename. if $varname = archer and $archer = 5 then $$varname = 5 Commented Aug 21, 2015 at 14:49
  • 2
    yeah, the ${"$thing"} notation is also correct, as is $$thing. I think the issue is the lack of the global statements as in my answer. Commented Aug 21, 2015 at 14:52
  • 1
    Note that with variable variablenames you have some magic behaviour. I would try to avoid such magical stuff for readability of the code. Commented Aug 21, 2015 at 14:58
  • 2
    Interesting. In all my years doing PHP, I've never come across ${"$variable"} syntax before. Thanks for the explanation! Commented Aug 21, 2015 at 16:04

2 Answers 2

1

You could use a construct like:

$s1 = "somevar";
$$s1 = "test";
echo $somevar; // will output: test

This in your code:

function army($troopData,$troop,$thing){
    global $barbarian;
    global $archer;
    global $giant;

    if(isset($troop) && $troopData+1 == $$thing){
    ...
}

Edit:

note that you should enable access to the $barbarian etc. variables inside the function!

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

1 Comment

In the EditCase you should get a warninng from php which says "$barbarian is not defined, assumed 'barbarian' " or something like this
0

For readability to avoid mistakes with the priority of oporaters add brackets like

if(isset($troop) && ( $troopData+1 == ${"$thing"})

In this case as a comment said you do not have to: http://php.net/manual/fa/language.operators.precedence.php

2 Comments

== has higher preceedence than &&, so leaving the parentheses out is safe. However, sometimes readability improves if you do use the superfluous parentheses...
@MartenKoetsier changed that. Sory!

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.