Creating Dynamic Pages

Contact Us or call 1-877-932-8228
Creating Dynamic Pages

Creating Dynamic Pages

Single Quotes vs. Double Quotes

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.

Compare the examples below.

Code Sample:

PhpBasics/Demos/SingleQuotes.php
<!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.Single Quotes Output

Code Sample:

PhpBasics/Demos/DoubleQuotes.php
<!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.Double Quotes Output

To see the effect of the special characters (\n and \t), you will have to view the source of the resulting page.

Passing Variables on the URL

A common way to pass values from the browser to the server is by appending them to the URL as follows:

Syntax

http://www.webucator.com/hello.php?greet=Hello&who=World

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.

Code Sample:

PhpBasics/Demos/HelloHi.html
<!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>

Code Sample:

PhpBasics/Demos/HelloHi.php
<?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.

  • Variable names begin with a dollar sign ($).
  • Values passed in the query string are part of the $_GET array and can be accessed using the following syntax: $_GET['fieldname'].
  • A shortcut for 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

Next