The delimiters, which tell the PHP interpreter where the start and end of the string are, are single and double quotes.
Everything that is inside the single quotes is printed. The single quotes are not part of the string. If you want to include a single quote inside a string, put a backslah (\) before the single quote inside the string. The backslash is called the escape character.
Double quote strings are simliar to single-quoted strings, but they have more special characters. The difference between singke-quoted and double-quoted string is that when a variable name is inside a double-quoted string, the value of the variable is substituted into the string, which doesn't happen with single-quoted strings.
Character | Description
|
---|
\n | new line |
\t | tab |
\r | carriage return |
\b | backspace |
\' | apostrophe or single quote |
\" | double quote |
\\ | backslash character (\) |
\$ | $ |
\XXX | the character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 777. For example, \251 is the octal sequence for the copyright symbol. |
\xXX | the character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol. |
There's a difference between displaying text at the command line and displaying text in a browser. You use HTML elements such as <br /> to format the text in a browser. To print at the command line, you use special characters such as "\n" to do the same.
You can also separate the items you want printed with commas (,).
If you want to assemble text strings together into one string, you can use a dot (.)
<html>
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
echo 'Hello World<br />';
echo 'We\'ll study hard.<br />';
echo 'Use a \\ to escape in a string.<br />';
print "The new line, tab, etc. are for the command line.<br />";
print "Hello World.\n";
print "We can display a \$ sign\tfor a currency.";
print "\251 \xA9<br /><br />";
echo "This is an example of using the commas:<br />";
echo "Hello", "welcome", "to PHP.<br />";
echo "To add spaces between words, do something like this:<br />";
echo "Hello ", "welcome ", "to PHP.<br /><br />";
echo "This is an example of using the dot:<br />";
echo "Hello ". "welcome ". "to PHP.<br />";
?>
</p>
</body>
</html>
|
View the effect