-1

I am trying to echo a string as a variable ($account_pk) but it just output's "$api_key1" and not the output of the variable, the 1 in $api_key1 is set when $key_num equals to 1.

Part of the code:

$key_num = $_POST['keynum'];
$account_pk = '$api_key' . $key_num;
echo $account_pk;

UPDATE: There is not a variable called $api_key I need $account_pk to combine the string $api_key with the variable $key_num then find a way to echo the real variable $api_key1

2 Answers2

1

just use double quotes instead of single quotes:

$account_pk = "$api_key" . $key_num;

Or like @Devon wrote, no quotes at all. Quotes make only sense if you mix a static string with varialbes, like this:

$text = "Hey $name, how are you?";

And even in this situation I prefer to write it this way

$text = "Hey ".$name." how are you?";

because for me it is more readable.

Mario A
  • 3,286
  • 1
  • 17
  • 22
1

I believe what the OP want is

$name = "api_key" . $key_num;
echo $$name;
invisal
  • 11,075
  • 4
  • 33
  • 54