As you have seen PHP recognises variable names inside quoted text. However sometimes you might want to use the dollar symbol ($) as currency not as a variable name or quotes as speech marks and not to hold string literals. You might also want to use quotation marks within the text. As an example create a new page called phpescape.php and place this in the lower block of PHP under the h1:
$stockitemname="skateboard"; $both="This $stockitemname is worth $thirty to which you might say "Wow, cheap"."; echo $both;
The string is being put into a variable and then displayed to the user. What you want to be displayed is this:
This skateboard is worth $thirty to which you might say "Wow, cheap".
Try it and you should get errors. Within that string are two main problems for PHP:
- a currency amount ($thirty) which will be recognised as a variable even though it is not
- words said by someone and therefore quoted but which PHP takes as the end of the quoted text (it then it gets confused and thinks that the next word Wow is PHP but it does not know it)
The first error may be fairly rare but the second is very common so get used to it. The fix is the same for both.
To fix the common problem first you need to escape the quotes which you want treated as part of the text and not the PHP:
$both="This $stockitemname is worth $thirty to which you might say \"Wow, cheap\".";
Save it and try it. Now the script works but with a notice which we fix next.
Other special symbols
Escape the dollar sign the way you did the quotes:
$both="This $stockitemname is worth \$thirty to which you might say \"Wow, cheap\".";
Try it and it should work now. Escaping is saying "The next character is text not part of the PHP code so just display it". You might also need to to escape braces and other characters with special meaning in PHP as you come across them. Watch out for "Unexpected" in error messages. You have either missed out quotes (semi-colons) or you have special characters in your strings.