ajetrumpet Posted March 29, 2021 Share Posted March 29, 2021 (edited) I don't think I've asked this before have I?? can someone give me an internet KB that gives me all scenarios that warrant using singles or doubles? as in, wrapping values, variables, and why I need to do either, and when, etc, etc....? thanks Edited March 29, 2021 by ajetrumpet Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/ Share on other sites More sharing options...
Barand Posted March 29, 2021 Share Posted March 29, 2021 https://www.php.net/manual/en/language.types.string.php Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/#findComment-1585485 Share on other sites More sharing options...
ajetrumpet Posted March 29, 2021 Author Share Posted March 29, 2021 (edited) Barand, that does not give me what I need. does it say what I was looking for? If it did, I must have missed it. here's an example of what I need an explanation of: why do the following 2 blocks of code work the same way and output the same string value, even though single and double quotes are being used for both? $var = 'value'; echo '$var'; echo 'value'; or am I incorrect? I did not test those examples, but I know I have written many instances of this type of thing in the past and it has worked. Edited March 29, 2021 by ajetrumpet Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/#findComment-1585489 Share on other sites More sharing options...
phppup Posted March 29, 2021 Share Posted March 29, 2021 From my experience (which is much less extensive than Barand's) double quotes as single quotes are mostly, but NOT ALWAYS, a matter of personal preference. There are definitely guidelines for dealing with strings and certain other specifics, but GENERALLY, either one will accomplish a task (as long as you remain consistent in your usage). To dissect your example: $var = 'value'; //since value is a non-numerical text the quotes are required echo 'value'; //simply tells PHP that you want the text inside the quotes to be displayed echo '$var'; //indicates that you want the item in the quotes (which translates to a variable value, in this case) to be displayed. echo " 'var' "; //would tell PHP to display the text value surrounded by the first set of quotes (the double quotes) and the $ will inform PHP to use the variable The expected result would be 'var' (although you may trigger an error bc you didn't handle the single quotes as special characters) Taken further, if you coded: echo "The variable 'var' is a test"; //it would display the exact sentence WITH the awkward spacing. echo 'The variable "var" is a test'; //would duplicate above BUT echo "The variable '$var' is a test"; //would INTEGRATE the text and the VARIABLE with the result of: The variable 'value' is a test echo "The variable ". $var . " is a test"; //would INTEGRATE the text and the VARIABLE with the result of: The variable value is a test Best if you play around with the variations on your own. And then follow up with error checking and handling of special characters. Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/#findComment-1585495 Share on other sites More sharing options...
ajetrumpet Posted March 29, 2021 Author Share Posted March 29, 2021 2 minutes ago, phppup said: echo '$var'; //indicates that you want the item in the quotes (which translates to a variable value, in this case) to be displayed. this is literally the only thing you said that I wasn't aware of. thanks! I think that pretty much gives me everything I need. your words to describe what PHP does what that line of code was really what I was after, more or less. wonderful! 😃 Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/#findComment-1585496 Share on other sites More sharing options...
phppup Posted March 29, 2021 Share Posted March 29, 2021 Glad I could help. Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/#findComment-1585497 Share on other sites More sharing options...
maxxd Posted March 30, 2021 Share Posted March 30, 2021 (edited) 10 hours ago, phppup said: echo '$var'; //indicates that you want the item in the quotes (which translates to a variable value, in this case) to be displayed. 10 hours ago, ajetrumpet said: this is literally the only thing you said that I wasn't aware of. thanks! I think that pretty much gives me everything I need. your words to describe what PHP does what that line of code was really what I was after, more or less. wonderful! 😃 Sorry, but this is completely incorrect. Single quotes in PHP will not interpolate variables, while double quotes will. So this: echo '$var'; will output $var , and echo "$var"; will output value Edited March 30, 2021 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/#findComment-1585513 Share on other sites More sharing options...
phppup Posted March 30, 2021 Share Posted March 30, 2021 @maxxd I stand corrected. Although I did say: Quote There are definitely guidelines for dealing with strings and certain other specifics..... I probably should have been more careful. Thanks for setting things straight. Quote Link to comment https://forums.phpfreaks.com/topic/312398-all-differences-with-using-single-quotes-and-double-quotes/#findComment-1585521 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.