Jump to content
Larry Ullman's Book Forums

Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...