如何将变量与下划线和其他单词组合在一起


How to combine a variable with a underscore and other word?

我需要将$variable与下划线和单词结合起来,如下所示:

$agent = 代理 ID字是"拒绝"。

$newvar = $agent_declined;

我该怎么做?

像这样: $newvar = $agent . "_declined";

在 PHP 中,您可以使用.来组合字符串

使用串联:

<?php
$newvar = $agent . "_declined";
?>

在这里阅读!

像这样?

$agent_declined = "foobar";
$id = "declined";
$varname = "'$agent_" . $id
$newvar = $$varname; // should give foobar

我建议不要使用双$$,这很令人困惑。

试试这个:

${$variableName.'_declined'} = 'foo';

有关更多信息,请参阅 PHP:变量变量