Well wrapping variables in strings with curly brackets has been my coding style since more than a year ago, but lately someone said it was bad. I have no idea why though, can anyone of you please explain to me about this?

Here is a sample code of a string I code in PHP:

$content = "{$user1} has donated {$money} dollars to {$user2}.";

Sure you may as well leave out the curly brackets, but whats wrong with them? Please lemme know.

    bradgrafelman;11003006 wrote:

    The color red is bad. The reason is because green is my favorite color.

    (In other words, it's a matter of style/preference.)

    Thank you so much for this quick comment. You really sure about it? This is what the advanced coder said regarding my usage of curly brackets:

    And the frequent use of curly brackets around variables was meant for certain conditions when using arrays, instead they seem to use it for cosmetic reasons.

    Maybe I am using it for cosmetic reasons, but the point is... Well, Id like to know if there is indeed a big problem regarding wrapping variables in curly brackets such as a large increase in memory usage. If not, then perhaps its indeed nothing but a matter of style preference.

      I see, so it is not really a problem after all. Thank you very much for all these, I appreciate. Guess I do not need to alter my programming style with curly brackets after all. XD

        Umm nobackseat? How did you know he was this advanced programmer I was referring to? Are you a member on Virtual petlist? Just wondering.

          sorry, overlooked parts of an earlier post. please ignore.

            I see, thats quite interesting to know. I've been improving my PHP skills a lot lately, but unfortunately still nowhere close to the level of advanced programmers. I hope someday I will, it will take a while though...

              The curly brackets are useful/needed if you want to have interpolated variables within a double-quoted (or heredoc) string, but where the parser would have trouble identifying the variable name for one reason or another.

              $foo = 1
              $foobar = 2;
              echo "{$foo}bar"; // '1bar'
              echo "$foobar"; // '2'
              

              It's also needed if you want to use quoted array keys within a string:

              echo "Hello, {$foo['world']}";
              echo "Hello, $foo['world']"; // parse error
              

              There are those who would argue for one reason or another against ever using variable interpolation within a string, instead advocating always using concatenation. Personally, I don't lose sleep over such things and instead use whatever seems clearest/simplest at the time (or sprintf() in some complex situation) -- I have more important things to worry about most of the time. 😉

                There's also other use-cases for them:

                $text = 'heading 1';
                
                echo "<img src=\"img_script.php?text={$text}_large\"/>";
                

                The braces help limit in what the PHP parser attempts to match as a variable that it needs to transpose in your script, without them, in this example, it would be attempting to find a variable labelled as $text_large which doesn't exist.

                  One reason to use curly brackets around none-array variables, is if you use them around arrays (like nog-dog shows). I personally always use brackets around variables of all types in HEREDOC but almost never in quoted strings, instead I concatenate those. The reason for this is because I like the appearance of variables with in the code to be consistent. For example:

                  $str = <<<END
                  My name is $name, John's age is {$Peopls['John']['age']}.
                  END;

                  Looks bad to me because the variables appear inconsistently. As brad pointed out originally tho, its all a matter of personal preference (or employer requirements).

                    Write a Reply...