Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brackets in PHP double-quoted strings [closed]

Tags:

string

php

this is more of a better-practice type of question.

I'd like to know if using brackets in double-quoted strings for variables is good practice.

For example:

<?php
$Variable = 'a variable';
$SingleQuotedString = 'Single quoted string with ' . $Variable;
// Single quoted string with a variable

$DoubleQuotedString = "Double quoted string with $Variable";
// Double quoted string with a variable

$DoubleQuotedStringWithBrackets = "Double quoted string with {$Variable} in brackets.";
// Double quoted string with a variable in brackets.
?>

It doesn't change the output or the code from simple tests, and obviously works. I'm just confused because not many people do this, and I don't see recommendations or people disagreeing with it, and I've been using them just fine.

Thanks for any feedback!

like image 370
Toxic Avatar asked Aug 23 '26 12:08

Toxic


1 Answers

The curly braces are to allow the use of arrays and objects, i.e:

$string = "my array value: {$foo['bar']}";

or

$string = "my object value: {$foo->bar}";
like image 126
Martin Avatar answered Aug 26 '26 02:08

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!