ManicMax Posted December 24, 2007 Share Posted December 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83036-php-syntax/ Share on other sites More sharing options...
revraz Posted December 24, 2007 Share Posted December 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83036-php-syntax/#findComment-422355 Share on other sites More sharing options...
ManicMax Posted December 24, 2007 Author Share Posted December 24, 2007 could someone explain the syntax for a SQL Query ??? Quote Link to comment https://forums.phpfreaks.com/topic/83036-php-syntax/#findComment-422358 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2007 Share Posted December 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83036-php-syntax/#findComment-422359 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.