PHP add Value to Variable in Variable before echo











up vote
-1
down vote

favorite












i am looking for a solution:



$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"


The variable "$place" can only be declared after the variable "$txt".
Is that possible?



Thanks










share|improve this question






















  • When $txt is set, it is concatenating "Welcome to ", the value of $place, and " City!" to form a new string. So no, you cannot. Why do you want to do this?
    – Jordan S
    Nov 21 at 19:45








  • 2




    Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
    – dan08
    Nov 21 at 19:48










  • You can't define a variable before using it.
    – Funk Forty Niner
    Nov 21 at 21:10















up vote
-1
down vote

favorite












i am looking for a solution:



$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"


The variable "$place" can only be declared after the variable "$txt".
Is that possible?



Thanks










share|improve this question






















  • When $txt is set, it is concatenating "Welcome to ", the value of $place, and " City!" to form a new string. So no, you cannot. Why do you want to do this?
    – Jordan S
    Nov 21 at 19:45








  • 2




    Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
    – dan08
    Nov 21 at 19:48










  • You can't define a variable before using it.
    – Funk Forty Niner
    Nov 21 at 21:10













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











i am looking for a solution:



$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"


The variable "$place" can only be declared after the variable "$txt".
Is that possible?



Thanks










share|improve this question













i am looking for a solution:



$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"


The variable "$place" can only be declared after the variable "$txt".
Is that possible?



Thanks







php variables echo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 19:42









SteveO24

11




11












  • When $txt is set, it is concatenating "Welcome to ", the value of $place, and " City!" to form a new string. So no, you cannot. Why do you want to do this?
    – Jordan S
    Nov 21 at 19:45








  • 2




    Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
    – dan08
    Nov 21 at 19:48










  • You can't define a variable before using it.
    – Funk Forty Niner
    Nov 21 at 21:10


















  • When $txt is set, it is concatenating "Welcome to ", the value of $place, and " City!" to form a new string. So no, you cannot. Why do you want to do this?
    – Jordan S
    Nov 21 at 19:45








  • 2




    Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
    – dan08
    Nov 21 at 19:48










  • You can't define a variable before using it.
    – Funk Forty Niner
    Nov 21 at 21:10
















When $txt is set, it is concatenating "Welcome to ", the value of $place, and " City!" to form a new string. So no, you cannot. Why do you want to do this?
– Jordan S
Nov 21 at 19:45






When $txt is set, it is concatenating "Welcome to ", the value of $place, and " City!" to form a new string. So no, you cannot. Why do you want to do this?
– Jordan S
Nov 21 at 19:45






2




2




Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48




Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48












You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10




You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10












3 Answers
3






active

oldest

votes

















up vote
1
down vote













It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:



$txt = "Welcome to %s City!";
$place = "New York";
printf($txt, $place); // should be "Welcome to New York City!"


https://3v4l.org/jEQvZ






share|improve this answer




























    up vote
    0
    down vote













    You can't do that directly. The string is concatenated using the value of $place at the time of assignment so any changes to $place will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.



    function welcome($place) {
    return 'Welcome to '.$place.' City!';
    }

    $place = 'New York';

    echo welcome($place);





    share|improve this answer




























      up vote
      0
      down vote













      Yes it's possible.

      Just create a placeholder and str_replace it.

      Notice I changed " to '.

      If you use " it will read $place as a variable. With ' $place is string and can be replaced later in the code.



      $txt = 'Welcome to $place City!';
      $place = "New York";
      echo str_replace('$place', $place, $txt);


      https://3v4l.org/DVtQa






      share|improve this answer





















      • Thank you! It works fine!
        – SteveO24
        Nov 21 at 19:57






      • 1




        @SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
        – Andreas
        Nov 21 at 20:00











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419450%2fphp-add-value-to-variable-in-variable-before-echo%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:



      $txt = "Welcome to %s City!";
      $place = "New York";
      printf($txt, $place); // should be "Welcome to New York City!"


      https://3v4l.org/jEQvZ






      share|improve this answer

























        up vote
        1
        down vote













        It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:



        $txt = "Welcome to %s City!";
        $place = "New York";
        printf($txt, $place); // should be "Welcome to New York City!"


        https://3v4l.org/jEQvZ






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:



          $txt = "Welcome to %s City!";
          $place = "New York";
          printf($txt, $place); // should be "Welcome to New York City!"


          https://3v4l.org/jEQvZ






          share|improve this answer












          It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:



          $txt = "Welcome to %s City!";
          $place = "New York";
          printf($txt, $place); // should be "Welcome to New York City!"


          https://3v4l.org/jEQvZ







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 19:55









          dan08

          5,30231430




          5,30231430
























              up vote
              0
              down vote













              You can't do that directly. The string is concatenated using the value of $place at the time of assignment so any changes to $place will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.



              function welcome($place) {
              return 'Welcome to '.$place.' City!';
              }

              $place = 'New York';

              echo welcome($place);





              share|improve this answer

























                up vote
                0
                down vote













                You can't do that directly. The string is concatenated using the value of $place at the time of assignment so any changes to $place will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.



                function welcome($place) {
                return 'Welcome to '.$place.' City!';
                }

                $place = 'New York';

                echo welcome($place);





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can't do that directly. The string is concatenated using the value of $place at the time of assignment so any changes to $place will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.



                  function welcome($place) {
                  return 'Welcome to '.$place.' City!';
                  }

                  $place = 'New York';

                  echo welcome($place);





                  share|improve this answer












                  You can't do that directly. The string is concatenated using the value of $place at the time of assignment so any changes to $place will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.



                  function welcome($place) {
                  return 'Welcome to '.$place.' City!';
                  }

                  $place = 'New York';

                  echo welcome($place);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 19:49









                  jfadich

                  3,0381519




                  3,0381519






















                      up vote
                      0
                      down vote













                      Yes it's possible.

                      Just create a placeholder and str_replace it.

                      Notice I changed " to '.

                      If you use " it will read $place as a variable. With ' $place is string and can be replaced later in the code.



                      $txt = 'Welcome to $place City!';
                      $place = "New York";
                      echo str_replace('$place', $place, $txt);


                      https://3v4l.org/DVtQa






                      share|improve this answer





















                      • Thank you! It works fine!
                        – SteveO24
                        Nov 21 at 19:57






                      • 1




                        @SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
                        – Andreas
                        Nov 21 at 20:00















                      up vote
                      0
                      down vote













                      Yes it's possible.

                      Just create a placeholder and str_replace it.

                      Notice I changed " to '.

                      If you use " it will read $place as a variable. With ' $place is string and can be replaced later in the code.



                      $txt = 'Welcome to $place City!';
                      $place = "New York";
                      echo str_replace('$place', $place, $txt);


                      https://3v4l.org/DVtQa






                      share|improve this answer





















                      • Thank you! It works fine!
                        – SteveO24
                        Nov 21 at 19:57






                      • 1




                        @SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
                        – Andreas
                        Nov 21 at 20:00













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Yes it's possible.

                      Just create a placeholder and str_replace it.

                      Notice I changed " to '.

                      If you use " it will read $place as a variable. With ' $place is string and can be replaced later in the code.



                      $txt = 'Welcome to $place City!';
                      $place = "New York";
                      echo str_replace('$place', $place, $txt);


                      https://3v4l.org/DVtQa






                      share|improve this answer












                      Yes it's possible.

                      Just create a placeholder and str_replace it.

                      Notice I changed " to '.

                      If you use " it will read $place as a variable. With ' $place is string and can be replaced later in the code.



                      $txt = 'Welcome to $place City!';
                      $place = "New York";
                      echo str_replace('$place', $place, $txt);


                      https://3v4l.org/DVtQa







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 21 at 19:49









                      Andreas

                      14.7k31441




                      14.7k31441












                      • Thank you! It works fine!
                        – SteveO24
                        Nov 21 at 19:57






                      • 1




                        @SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
                        – Andreas
                        Nov 21 at 20:00


















                      • Thank you! It works fine!
                        – SteveO24
                        Nov 21 at 19:57






                      • 1




                        @SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
                        – Andreas
                        Nov 21 at 20:00
















                      Thank you! It works fine!
                      – SteveO24
                      Nov 21 at 19:57




                      Thank you! It works fine!
                      – SteveO24
                      Nov 21 at 19:57




                      1




                      1




                      @SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
                      – Andreas
                      Nov 21 at 20:00




                      @SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
                      – Andreas
                      Nov 21 at 20:00


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419450%2fphp-add-value-to-variable-in-variable-before-echo%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Berounka

                      Sphinx de Gizeh

                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...