开发者

PHP - Variable inside variable?

开发者 https://www.devze.com 2022-12-26 03:09 出处:网络
$bookA = \"123\"; $开发者_C百科crack = \"A\"; I want to do something similar to this: echo $book$crack;

$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

精彩评论

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