PHP - Variable inside variable?

前端 未结 6 1101
梦毁少年i
梦毁少年i 2021-01-18 07:00

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

I want to do something similar to this:

echo $book$crack;

Such that the output is 123

相关标签:
6条回答
  • 2021-01-18 07:08

    This will work:

    $bookA = "123";
    $crack = "A";
    $var = "book$crack";
    echo $$var;
    
    0 讨论(0)
  • 2021-01-18 07:12

    You might want to use an associative array.

    For instance:

    $book = array();
    $book["A"] = "Some Book";
    $crack = "A";
    
    //Later
    echo $book[$crack];
    
    0 讨论(0)
  • 2021-01-18 07:22

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

    0 讨论(0)
  • 2021-01-18 07:23
    echo ${"book" . $crack};
    
    0 讨论(0)
  • 2021-01-18 07:29

    Try the following:

    echo ${book.$crack};
    

    It works for me.

    0 讨论(0)
  • 2021-01-18 07:30
    $varname = 'book'.$crack;
    echo $$varname;
    
    0 讨论(0)
提交回复
热议问题