PHP has one really annoying challenge. It uses quotes around any text it uses. Unfortunately that text might include HTML or JavaScript which will also contain quotes. It needs a way to tell PHP quotes from quotes which should just be ECHOed to the Web page.
Single quotes
Single quotes are quicker to process and allow you to easily put HTML into echo and other statements:
echo '<p class="blue">';
Everything inside the single quotes is treated as a string literal. That is, something to be used but not processed as code.
Double quotes
Double quotes have a big advantage over single quotes. They allow you to embed other things inside the strings (as you will see on the next page).
When you want to use PHP to echo a string which includes double quotes you use an escape character. So to echo this chunk of HTML:
<p class="blue">
You would use this PHP code:
echo "<p class=\"blue\">";
Remember that ECHO is used to output something to the Web page which is being assembled.
In an actual Web page this could look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
.blue {
color:blue;
}
</style>
<title>Hello world as text</title>
</head>
<body>
<?php
echo "<p class=\"blue\">Hello world</p>";
?>
</body>
</html>
The backslash tells the server to treat the next character like text and not part of the PHP code. It can be used to escape any character including another backslash. If you need to use a backslash in the Web page text you escape it with a backslash or the server will think it is an escape character and ignore the next character.
You also need to escape $ as that is used in PHP to identify a variable.
If you had not escaped the HTML quotation marks:
echo "<p class="blue">";
the server would think that the first HTML quote was finishing what was supposed to be ECHOed.
Conclusion
Quotes can often get very confusing as you can have HTML quotes, JavaScript quotes and PHP quotes all in the same line of code. You can use either single or double quotes as you prefer but basically you just need to be careful and disciplined.
For reasons which may become clear on the next page this beginners' tutorial sticks with double quotes.
Try adding some lines ECHOing text, HTML and quotes of different types into firstphppage.php. They should go within the <?php and ?> tag. Then upload or copy the page to your server and access it in a browser. View trhe source HTML to see what happened to each one. Try to understand the use of quotes before you move on. Don't worry it gets easier!



