tags:

views:

84

answers:

5

Hey guys...

$bookA = "123";
$crack = "A";

I want to do something similar to this:

echo $book$crack;

Such that the output is 123.

What is the correct syntax for the echo command?

Thanks.

+2  A: 
$varname = 'book'.$crack;
echo $$varname;
Milan Babuškov
+5  A: 

These are called variable variables, but you should use arrays instead.

Progman
Why do you say he should use arrays, when we did not explain why he needs to do it this way. Maybe the data is coming from the source he has no control of. Arrays are completely irrelevant to the question.
Milan Babuškov
Because arrays have more features (such as being easy to iterate over) and are much more readable in code.
David Dorward
"Maybe the data is coming from the source he has no control of." - because evaluating the 3rd party data is terrible practice. 3rd party data should never interact with real names of variables/functions/whatever - the only possible way to interaction is to work with data.
zerkms
+7  A: 
echo ${"book" . $crack};
Chacha102
Nice. Learned something new today.
zaf
+2  A: 

This will work:

$bookA = "123";
$crack = "A";
$var = "book$crack";
echo $var;
Josh
Why was this downvoted?
Josh
+3  A: 

You might want to use an associative array.

For instance:

$book = array();
$book["A"] = "Some Book";
$crack = "A";

//Later
echo $book[$crack];
Joshua Rodgers
Using `“` and `”` instead of `"` may lead to weird parse errors ;)
Progman
Fixed that, sorry. Didn't realize that those snuck in there. :P
Joshua Rodgers