Jump to content

Newbie question about single quotes, double quotes, and {}


LazerOrca

Recommended Posts

Hello everyone!  This is my first post.  I am very new to php and mysql and coding in general.  It has not been made 100% clear to me as to when I should use single quotes, double quotes, and {}.  From what I gather you use single quotes for literal interpretation...  so if you put something like a variable in single quotes and echoed it, it would literally echo it as it is written and not the value of the variable.  in double quotes, I gather that it will echo the value of the variable.  as for {} I am unclear as to when to use the curly brackets for a variable.  I am assuming if you had a statement in single quotes and you put a variable in curly brackets you would get the value of the variable?

Edited by LazerOrca
Link to comment
Share on other sites

Don't know if I know what is best, but I could tell you how I do it.

 

Single quotes when not much to insert or if the inserting stuff is constants or static properties.

 

Double quotes and always put curly brackets around it even when not necessary.

 

For the official answer, see http://php.net/manual/en/language.types.string.php.

Link to comment
Share on other sites

Within single quotes php does not get parsed.

Within double quotes php does get parsed, but if you try to insert a single quoted argument such as $_POST['value']...then you have to concatenate (escape it).

While curly braces/brackets are used to escape variable expressions, it can also be used to concatenate. (only within double quotes)

Link to comment
Share on other sites

$arr1 = [ [4,5], [7,9,5], [1,2] ];
$arr2 = ['A', 'B', 'C'];
$arr3 = ['A' => 123,  'B'=> 456];
$simple = 'xyz';

$txt = "Print simple var like $simple or $arr2[2] or $arr3[B], but complex var needs curlies as in {$arr1[1][2]}";

echo $txt;

/**** OUTPUT ****
Print simple var like xyz or C or 456, but complex var needs curlies as in 5
*/

NOTE: if using $arr3['B'] on its own e.g.

echo $arr3['B'];

the quotes around 'B' are required (it will work but inefficiently as PHP will first search for a defined constant 'B' and interpret it as a string when one is not found), but not if it is inside a double-quoted string.

 

If you use "$arr3['B']" inside double quotes then it too needs curlies.

 

Simple vars may also need curlies in some cases to prevent ambiguity in the var name e.g.

$day = 26;
echo "Today is the {$day}th of November";

As a rule, I use curlies around all array and object expressions but not around simple variables (unless required as in the last example)

Edited by Barand
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.