I have following scenario.I am creating a config page where I set the format in one place. I am using this format in more than 10 php pages.
//Settings
$format = "$one $two $three";
$one = 1;
$two = 2;
$three = 3;
echo $format;
//This should output 1 2 3
Right now if I want to change the format I need to change "$two $one $three"
in all 10 pages ,, how can I set this in one place and reuse it in multiple place.Is this possible in php ?
P.s :
My scenario : I have settings.php , where I set $format = "$one $two $three";
, I am including settings.php in all 10 pages.... When I change $format
in settings.php it should should be reflected in all 10 pages..without much work.
You should write a function which creates the format according to your needs:
function createFormat($one, $two, $three)
{
return "$one $two $three";
}
Then, whereever you need the format, you just write:
$format = createFormat($one, $two, $three);
You can use an callable do it better (in settings.php):
//define the function first, with references
$one = 0; $two = 0; $three = 0;
$format = function () use (&$one,&$two,&$three) { print "$one $two $three";};
In the next file you do
$one = 1; $two = 2; $three = 3;
$format();//will print "1 2 3"
$one = 2; $two = 5; $three = 6;
$format();//will print "2 5 6"
This can work, but you have to keep an eye on the used references (variables)
inline variable parsing:
$one=1; $two=2; $three=3;
$format = "{$one} {$two} {$three}";
return $format;