Jump to content

all differences with using single quotes and double quotes


ajetrumpet

Recommended Posts

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

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

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.

Link to comment
Share on other sites

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!  😃

Link to comment
Share on other sites

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 by maxxd
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.