Jump to content

When should complex (curly) string syntax be used?


NotionCommotion

Recommended Posts

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";
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by NotionCommotion
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.