Jump to content

Difference Between "" and ''


oliverj777

Recommended Posts

Single quotes cause everything within them to be interpreted as literal strings.

= is an assignment operator, used to assign a value,

== is a comparison operator, used to compare values

$pie = 'blueberry';
echo "I have a $pie pie"; // will return "I have a blueberry pie", whereas
echo 'I have a $pie pie'; // will return "I have a $pie pie".
if( $pie == 'blueberry') {
     echo 'The pie is blueberry';
} else {
     echo 'The pie is NOT blueberry';
}

Link to comment
Share on other sites

Hi

 

In "" variables will be interpreted (so "Hello $User" would put be "Hello Fred" is $User was equal to Fred), which will not be done in ''.

 

= is an assignment, == is used to check a variable (and === is used to check a variable ensuring type, such as when checking true and false).

 

All the best

 

Keith

Link to comment
Share on other sites

text between single quotes is just only plain (html-)text, text between dubblequotes have a littlebit more richness, for example, you can place variables within dubblequotes and they will work, as in singlequotes i will just appear as text. Also you can use some extra layout for the text like codes as "\n" wich will work as <br>.

 

for myself, i like to work as much as possible with only singlequotes when just working with plain html codes and text, but that differs between people

 

the difference between = and == is very easy, the first is giving a variable a surtain value, the second is testing if a given variable is allready containing a surtain value

 

$variable = 'hello world';

$variable contains now the text: 'hello world'

 

if ($variable == 'hello world')
{
echo 'the variable contains the text: hello world!'
}

this code is testing if the variable is containing the text 'hello world',  if so, the text 'the variable contains the text: hello world!' is displaid

Link to comment
Share on other sites

Double quotes have the ability to parse the variables inside them..

Single quotes do not, they will contain whatever you put in them.

 

Nevertheless, whichever quotes you do decide to use, if you want to put one of the same quotes within the string, you will have to escape it with the backslash

 

$str = "Sometime I like to put quotes around a \"$variable\"";

The same goes for single quotes.

 

Nevertheless, Pickachu said it best.

Link to comment
Share on other sites

So if I were to say something like this:

 

if($field = ''){
echo "Please fill in the field;
}

 

Does that look correct?

 

 

No. That will always evaluate to TRUE since you're assigning, not comparing values.

 

if( $field == '') would be the correct way to compare values.

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.