nootkan Posted August 8, 2014 Share Posted August 8, 2014 Larry in the book on page 59 where it states: 4. Print out the user information: print "<p>Thank you, $title $name, for your comments.</p><p>You stated that you found this example to be '$response' and added:<br />$comments</p>"; There are no single quotes in the first two variables ($title $name) and last variable ($comments). Is that by design or a typeO? I noticed that it works either way but wanted to know if that is okay to do or should they all have the single quotes? Just a little confusing for me. Link to comment Share on other sites More sharing options...
Larry Posted August 8, 2014 Share Posted August 8, 2014 It just a matter of whether you want to print single quotes or not. It'd be odd to display a person's name and title within quotes. Link to comment Share on other sites More sharing options...
nootkan Posted August 9, 2014 Author Share Posted August 9, 2014 I thought all variables had to have either single or double quotes. I must be not understanding something. I'll go back and read again. Link to comment Share on other sites More sharing options...
Antonio Conte Posted August 9, 2014 Share Posted August 9, 2014 Quote types work a little bit different. If you want a variable to work inside quotes, you eighter needs to use double quotes or use concatination. $var = "HERE"; echo "You can echo out '{$var}' using double quotes"; // You can echo out 'HERE' using double quotes echo 'But you can't echo $var using single-quites'; // But you can't echo $var using single quotes echo 'Unless you concatinate. Then ' . $var . ' will work'; // Unless you concatinate. Then HERE will work. The reason why $response is wrapped in single-quotes in your example is that he wanted to display the single quotes in the output itself. The {}-tags are only a precaution on my side, btw. They explicitly states it's a variable. Link to comment Share on other sites More sharing options...
nootkan Posted August 9, 2014 Author Share Posted August 9, 2014 Oh I see so it's not actually a variable value unless it is in brackets and variables can be used by themselves without quotes unless the output requires them. Thanks for your help. Link to comment Share on other sites More sharing options...
Larry Posted August 10, 2014 Share Posted August 10, 2014 Yes -> "variables can be used by themselves without quotes unless the output requires them". No -> "it's not actually a variable value unless it is in brackets" As Antonio said in his post, those brackets weren't necessary. A variable is a variable if it's a variable. If you want to use the variable's value within a string, you must use double quotes. That's the main thing to grasp. Link to comment Share on other sites More sharing options...
Recommended Posts