0

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.

4
  • This will work if you move the $format = line after $three =. Commented Mar 16, 2017 at 12:56
  • So you define a constant in a file and include it on every page or you can define it a function and call it on each page Commented Mar 16, 2017 at 12:56
  • my scenario is I have settings.php , where I set $format = "$one $two $three"; , I am including settings.php in all 10 pages.... Commented Mar 16, 2017 at 12:57
  • @Vishnu u have an answer below Commented Mar 16, 2017 at 13:00

3 Answers 3

4

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);
Sign up to request clarification or add additional context in comments.

3 Comments

This is the best and probably only real way of achieving what you're after.
Thanks , My brain was not working lol , will accept answer soon
This should do it
1

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)

Comments

0

inline variable parsing:

    $one=1; $two=2; $three=3;  
    $format = "{$one} {$two} {$three}";  
    return $format;  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.