Php 在变量内部使用变量
我有以下场景。我正在创建一个配置页面,在其中一个位置设置格式。我在10多个php页面中使用这种格式Php 在变量内部使用变量,php,Php,我有以下场景。我正在创建一个配置页面,在其中一个位置设置格式。我在10多个php页面中使用这种格式 //Settings $format = "$one $two $three"; $one = 1; $two = 2; $three = 3; echo $format; //This should output 1 2 3 现在,如果我想更改格式,我需要在所有10个页面中更改“$two$one$three”,我如何在一个位置设置它,并在多个位置重用它。在php中可以这样做吗 附言: 我的
//Settings
$format = "$one $two $three";
$one = 1;
$two = 2;
$three = 3;
echo $format;
//This should output 1 2 3
现在,如果我想更改格式,我需要在所有10个页面中更改“$two$one$three”
,我如何在一个位置设置它,并在多个位置重用它。在php中可以这样做吗
附言:
我的场景:我有settings.php,其中我设置了
$format=“$1$2$3”代码>,我将settings.php包含在所有10页中。。。。当我在settings.php中更改$format
时,它应该反映在所有10页中。无需做太多工作。您应该编写一个函数,根据需要创建格式:
function createFormat($one, $two, $three)
{
return "$one $two $three";
}
然后,无论您需要什么格式,只要写:
$format = createFormat($one, $two, $three);
您应该编写一个函数,根据需要创建格式:
function createFormat($one, $two, $three)
{
return "$one $two $three";
}
然后,无论您需要什么格式,只要写:
$format = createFormat($one, $two, $three);
您可以使用可调用的更好的方法(在settings.php中):
在下一个文件中,您需要
$one = 1; $two = 2; $three = 3;
$format();//will print "1 2 3"
$one = 2; $two = 5; $three = 6;
$format();//will print "2 5 6"
这是可行的,但您必须注意使用的引用(变量)您可以使用可调用的do it better(在settings.php中):
在下一个文件中,您需要
$one = 1; $two = 2; $three = 3;
$format();//will print "1 2 3"
$one = 2; $two = 5; $three = 6;
$format();//will print "2 5 6"
这是可行的,但您必须注意使用的引用(变量)内联变量解析:
$one=1; $two=2; $three=3;
$format = "{$one} {$two} {$three}";
return $format;
内联变量解析:
$one=1; $two=2; $three=3;
$format = "{$one} {$two} {$three}";
return $format;
如果您将$format=
行移动到$three=
之后,这将起作用。因此,您可以在文件中定义一个常量,并将其包含在每个页面上,或者您可以将其定义为一个函数,并在每个页面上调用它。我的场景是I have settings.php,其中我设置了$format=“$one$two$three”,我将settings.php包含在所有10页中。@Vishnu下面有一个答案。如果您将$format=
行移到$three=
之后,这将起作用。因此,您可以在文件中定义一个常量,并将其包含在每页中,或者您可以将其定义为函数,并在每页上调用它。我的场景是我有settings.php,其中我设置了$format=“$1$2$3"; , 我将settings.php包含在所有10页中。@Vishnu下面有一个答案。这是实现你所追求的目标的最好、可能是唯一真正的方法。谢谢,我的大脑不工作了,哈哈,我会接受答案的。这应该可以做到。这是实现你所追求的目标的最好、可能是唯一真正的方法。谢谢,我的大脑不工作了,哈哈,我会很快接受你的回答,我应该这样做