0

I'm trying to refer to a variable that's made up from a string, so say $type = "pistol", then:

if ($number > $($type. "s_XP")) 

would be

if ($number > $pistols_XP) 

Obviously the top one isnt correct syntax, because I don't know how it should be written. Any help?

3 Answers 3

2

The correct syntax would be:

if ($number > ${$type . "s_XP"}) 

However, you should be storing this data in an object instead (or perhaps an array):

$pistol = new Weapon(50);
$nuke = new Weapon(9001);

$type = $pistol;

if($number > $type->XP)
Sign up to request clarification or add additional context in comments.

2 Comments

Also: $var = $type . "s_XP"; if( $number > $$var ) ...
$type is actually pulled from a database as $rs[type], just simplified it for the question. thanks
1

Close.

if ($number > ${$type."s_XP"})

But you should be using arrays instead.

Comments

1

Correct syntax:

if ($number > $data["pistol"]["xp"]) 

learn to use arrays

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.