NotionCommotion Posted June 11, 2016 Share Posted June 11, 2016 I've read the manual http://php.net/manual/en/language.types.string.php#language.types.string.parsing, and know when it has to be used. When should it be used? For instance, is $url1 or $url2 considered better practice? Are their other common scenarios where complex syntax is not required, but should be used? $this->a='aaa'; $arr['c']='ccc'; $url1="$this->a/foo/$arr[c]/bar"; $url2="{$this->a}/foo/{$arr['c']}/bar"; Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/ Share on other sites More sharing options...
requinix Posted June 11, 2016 Share Posted June 11, 2016 The complex syntax in $url2 is more or less universally recommended. Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533578 Share on other sites More sharing options...
Jacques1 Posted June 11, 2016 Share Posted June 11, 2016 When should it be used? Have you tried answering that yourself? Compare the two in terms of readability and consistency (array index syntax), and I'm sure you'll figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533579 Share on other sites More sharing options...
NotionCommotion Posted June 11, 2016 Author Share Posted June 11, 2016 The complex syntax in $url2 is more or less universally recommended. I agree it is easier to read and less prone to errors, and have recently made an effort of using it for anything in the slightest ambiguous. I am starting to think even that might be too lax, and leads to inconsistencies. Maybe a better question is whether you believe simple syntax should ever be used? Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533580 Share on other sites More sharing options...
NotionCommotion Posted June 11, 2016 Author Share Posted June 11, 2016 (edited) Have you tried answering that yourself? Compare the two in terms of readability and consistency (array index syntax), and I'm sure you'll figure it out. Hi Jacques1, I've already made a change to using it when arrays and objects are involved. I should have asked whether something like this is considered bad practice. If not, where should one draw the line? $color="brown"; $string="The $color cow jumped over the moon."; Edited June 11, 2016 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533581 Share on other sites More sharing options...
Jacques1 Posted June 11, 2016 Share Posted June 11, 2016 Again: I think you can answer that yourself. In your $color example, there are no readability issues, no ambiguity, no complex expression. The curly braces wouldn't add anything, so they're not required. You can still use them for the sake of consistency, but you don't have to. The simple syntax works just fine. Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533582 Share on other sites More sharing options...
NotionCommotion Posted June 11, 2016 Author Share Posted June 11, 2016 Again: I think you can answer that yourself. In your $color example, there are no readability issues, no ambiguity, no complex expression. The curly braces wouldn't add anything, so they're not required. You can still use them for the sake of consistency, but you don't have to. The simple syntax works just fine. I can answer what I think is unambiguous, but I can't answer what the majority of PHP programmers consider proper, and I would like to be consistent with the greater majority (of good coders). Would you say the only time it is acceptable is when the variable has either white space or a double quote on either side? Furthermore, only simple variables and maybe objects, but not arrays? Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533583 Share on other sites More sharing options...
Jacques1 Posted June 11, 2016 Share Posted June 11, 2016 I can answer what I think is unambiguous, but I can't answer what the majority of PHP programmers consider proper, [...] Just like requinix or me. Our opinion on this matter isn't any more relevant than yours. We don't have any secret knowledge about string interpolation. Would you say the only time it is acceptable is when the variable has either white space or a double quote on either side? Pretty much, yes. Furthermore, only simple variables and maybe objects, but not arrays? There are dozens of edge cases we can discuss all day long, but that's probably a waste of time. I think the previous answer was very clear: Use curly braces except for very simple cases. Now you can decide what you think is simple. How often do you need to insert raw variables into strings, anway? The last time I did that was when I assembled the message for an exception. Everything else is handled by the template engine or requires some form of escaping or is too complex for the very limited string interpolation in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533585 Share on other sites More sharing options...
NotionCommotion Posted June 12, 2016 Author Share Posted June 12, 2016 Just like requinix or me. Our opinion on this matter isn't any more relevant than yours. We don't have any secret knowledge about string interpolation. I think both of you do Pretty much, yes. Perfect. How often do you need to insert raw variables into strings, anway? Exceptions, validation, some URL manipulation "yikes" and "even more yikes" dynamic SQL. Quote Link to comment https://forums.phpfreaks.com/topic/301327-when-should-complex-curly-string-syntax-be-used/#findComment-1533586 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.