PHP add Value to Variable in Variable before echo












-1















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 '18 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

    – Dan
    Nov 21 '18 at 19:48











  • You can't define a variable before using it.

    – Funk Forty Niner
    Nov 21 '18 at 21:10
















-1















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 '18 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

    – Dan
    Nov 21 '18 at 19:48











  • You can't define a variable before using it.

    – Funk Forty Niner
    Nov 21 '18 at 21:10














-1












-1








-1








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 '18 at 19:42









SteveO24SteveO24

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 '18 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

    – Dan
    Nov 21 '18 at 19:48











  • You can't define a variable before using it.

    – Funk Forty Niner
    Nov 21 '18 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 '18 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

    – Dan
    Nov 21 '18 at 19:48











  • You can't define a variable before using it.

    – Funk Forty Niner
    Nov 21 '18 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 '18 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 '18 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

– Dan
Nov 21 '18 at 19:48





Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is

– Dan
Nov 21 '18 at 19:48













You can't define a variable before using it.

– Funk Forty Niner
Nov 21 '18 at 21:10





You can't define a variable before using it.

– Funk Forty Niner
Nov 21 '18 at 21:10












3 Answers
3






active

oldest

votes


















1














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































    0














    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































      0














      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 '18 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 '18 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',
      autoActivateHeartbeat: false,
      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









      1














      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




























        1














        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


























          1












          1








          1







          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 '18 at 19:55









          DanDan

          5,44531432




          5,44531432

























              0














              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




























                0














                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


























                  0












                  0








                  0







                  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 '18 at 19:49









                  jfadichjfadich

                  3,4381723




                  3,4381723























                      0














                      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 '18 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 '18 at 20:00
















                      0














                      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 '18 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 '18 at 20:00














                      0












                      0








                      0







                      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 '18 at 19:49









                      AndreasAndreas

                      16.5k41744




                      16.5k41744













                      • Thank you! It works fine!

                        – SteveO24
                        Nov 21 '18 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 '18 at 20:00



















                      • Thank you! It works fine!

                        – SteveO24
                        Nov 21 '18 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 '18 at 20:00

















                      Thank you! It works fine!

                      – SteveO24
                      Nov 21 '18 at 19:57





                      Thank you! It works fine!

                      – SteveO24
                      Nov 21 '18 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 '18 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 '18 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.




                      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







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini