In PHP, for simple strings you can use single quotes and double quotes interchangeably. However, there is one important difference of which you need to be aware. Text within single quotes will not be parsed for variables and escape sequences
Escape sequences are used for characters that cannot easily be output within strings. Common escape sequences are \n for a newline, \t for a tab, \\ for a backslash, \" for a double quote, and \$ for a dollar sign.
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Single Quotes</title> </head> <body> <?php $person = 'George'; echo '\tHello\n$person!!'; ?> </body> </html>
Because of the use of single quotes above, the string "
\tHello\n$person!!" will be output literally, as shown below.
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Single Quotes</title> </head> <body> <?php $person = "George"; echo "\tHello\n$person!!"; ?> </body> </html>
This time, because of the double quotes, the string will be parsed for variables and special characters and will be output as shown below.
To see the effect of the special characters (
\t), you will have to view the source of the resulting page.
A common way to pass values from the browser to the server is by appending them to the URL as follows:
The part of the URL that follows the question mark is called the query string. One or more name-value pairs can be passed to the server in this way. Each name-value pair is separated by an ampersand (
&). The processing page can read these name-value pairs and use them to determine its response.
The HTML page below shows an example of how these name-value pairs might be passed.
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Preferred Greeting</title> </head> <body> Do you prefer a formal greeting or an informal greeting? <ul> <li><a href="HelloHi.php?greet=Hello">Formal</a></li> <li><a href="HelloHi.php?greet=Hi">Informal</a></li> <li><a href="HelloHi.php?greet=Howdy">Friendly</a></li> </ul> </body> </html>
<?php //Assign the passed variable to a variable with //a more convenient name. $greeting = $_GET['greet']; ?> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title><?= $greeting ?> World!</title> </head> <body> <?php echo "$greeting World!"; ?> </body> </html>
Notice the following about the code above.
$_GETarray and can be accessed using the following syntax:
echo 'text to print';is
<?= 'text to print' ?>.
Many PHP developers feel that it is best practice to avoid using this shortcut syntax for
echo. One reason for this is that the shortcut syntax makes the resulting PHP file impossible to parse as XML. Another is that the short_open_tag directive must be turned on for it to work. See http://us3.php.net/manual/en/ini.core.php#ini.short-open-tag