Omirion Posted January 25, 2010 Share Posted January 25, 2010 Sup, Currently reading a book for PHP 5 and i can't understand why for example. echo "$var1 - $var2<br />'; Why is the starting quote a double and the closing one a single? and is there a difference? Quote Link to comment https://forums.phpfreaks.com/topic/189715-newbie-syntax-question/ Share on other sites More sharing options...
manwhoeatsrats Posted January 25, 2010 Share Posted January 25, 2010 Sup, Currently reading a book for PHP 5 and i can't understand why for example. echo "$var1 - $var2<br />'; Why is the starting quote a double and the closing one a single? and is there a difference? well if you have a something that begins with a " you have to have the same ending for that statement. just like a ' has to have an ending. this is really very useful for something like a SQL query $query = "UPDATE table_name SET colum1 = '10' WHERE colum2 = 'pie' "; as you can see at the beginning of the query I have double quotes then I have another double quotes at the end. any values in the query have single. quotes. Basically this allows you to have quotes inside of quotes. I hope this makes sense... Quote Link to comment https://forums.phpfreaks.com/topic/189715-newbie-syntax-question/#findComment-1001221 Share on other sites More sharing options...
iScript Posted January 25, 2010 Share Posted January 25, 2010 Yes, there is a different: $string1 = "Hi"; echo "The string1 var is $string1"; // output: the string1 var is Hi // Its not recommended to so that, the best way is to put a dot to seperate strings and vars echo 'The string1 var is $string1'; // output: the string1 var is $string1 // Good way: echo "The string1 var is " . $string1; // output: the string1 var is Hi echo 'The string1 var is ' . $string1; // output: the string1 var is Hi Another good thing to have this two is this: echo "<span style="color:red">spanInnerHTML</span>"; // ERROR: double quotes within double quotes... // Solution: echo "<span style=\"color:red\">spanInnerHTML</span>"; // output: <span style="color:red">spanInnerHTML</span> echo "<span style='color:red'>spanInnerHTML</span>"; // output: <span style='color:red'>spanInnerHTML</span> Quote Link to comment https://forums.phpfreaks.com/topic/189715-newbie-syntax-question/#findComment-1001223 Share on other sites More sharing options...
gwolgamott Posted January 25, 2010 Share Posted January 25, 2010 Another way of thinking of it is that it really isn't a difference. You can either use single or double quotes. But personally just to make it look cleaner for my own navigation of my code, and totally only my own preference, I tend to stick with double quotes and do something like this instead of how they use it... not withstanding certain cases of course that call for using single quotes... and you'll run into those as you play with PHP more trust me. Anyways it doesn't really matter (most of the time that is, but windows servers tend to not like you mixing it... see Iscripts examples for soemthing I'm pretty sure windows servers puke on with php ) but I like to do that specific code they gave you in this manner instead: echo $var1 - $var2."<br />"; or like this echo $var1 - $var2.'<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/189715-newbie-syntax-question/#findComment-1001227 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2010 Share Posted January 25, 2010 Why is the starting quote a double and the closing one a single? If the line of code you posted is the entire code, then it is a mis-print in the book because that is invalid syntax. Quote Link to comment https://forums.phpfreaks.com/topic/189715-newbie-syntax-question/#findComment-1001229 Share on other sites More sharing options...
Omirion Posted January 25, 2010 Author Share Posted January 25, 2010 Why is the starting quote a double and the closing one a single? If the line of code you posted is the entire code, then it is a mis-print in the book because that is invalid syntax. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/189715-newbie-syntax-question/#findComment-1001568 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.