开发者

PHP - Variable inside variable?

$bookA = "123"; $开发者_C百科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.


echo ${"book" . $crack};


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


$varname = 'book'.$crack;
echo $$varname;


You might want to use an associative array.

For instance:

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

//Later
echo $book[$crack];


This will work:

$bookA = "123";
$crack = "A";
$var = "book$crack";
echo $$var;


Try the following:

echo ${book.$crack};

It works for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜