Jump to content

Recommended Posts

What is the difference between

 

$var = "content";

 

$var = 'content';

 

I read somewhere that double quotes should be used if you are defining another variable within something and single quotes if not. The correct use also helps increase efficiency, but of course only on a very small scale.

 

Something has puzzeled me though as for some of my script some of the querys have ended up like this

 

$sql = "SELECT * FROM table WHERE field='$field'";

 

How come everything is in double quotes but the variable is in single ? and still functions.

 

Any and all help is appreciated.

;D

Link to comment
https://forums.phpfreaks.com/topic/83036-php-syntax/
Share on other sites

In PHP, single quotes and double quotes are basically the same except when there is a $varialbe inside.

 

If you use single quotes, then the string is parsed exactly as it was assigned.  If you use double quotes, then the string is parsed and any $variables are parsed with the value in the quotes.

 

In a SQL statement, these rules are not the same.

Link to comment
https://forums.phpfreaks.com/topic/83036-php-syntax/#findComment-422355
Share on other sites

PHP also understands and will parse escape sequences for special characters inside double-quoted strings. Such as \n.

 

The $sql = "..."; example is a php string and obeys all the php string rules.

 

Because the overall string is enclosed in double-quotes, any variables in it are parsed and replaced with their content. The single-quotes contained within the double-quoted string are literally just single-quote characters. They are needed to make what they are enclosing (in this case the contents of the php variable after it is parsed) a quoted-string, which sql expects around a string.

Link to comment
https://forums.phpfreaks.com/topic/83036-php-syntax/#findComment-422359
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.