LazerOrca Posted November 26, 2014 Share Posted November 26, 2014 (edited) 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 November 26, 2014 by LazerOrca Quote Link to comment https://forums.phpfreaks.com/topic/292716-newbie-question-about-single-quotes-double-quotes-and/ Share on other sites More sharing options...
NotionCommotion Posted November 26, 2014 Share Posted November 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292716-newbie-question-about-single-quotes-double-quotes-and/#findComment-1497697 Share on other sites More sharing options...
QuickOldCar Posted November 26, 2014 Share Posted November 26, 2014 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) Quote Link to comment https://forums.phpfreaks.com/topic/292716-newbie-question-about-single-quotes-double-quotes-and/#findComment-1497709 Share on other sites More sharing options...
Barand Posted November 26, 2014 Share Posted November 26, 2014 (edited) $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 November 26, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/292716-newbie-question-about-single-quotes-double-quotes-and/#findComment-1497713 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.