Differences Between Types of Quotation Marks

 

Introduction.  You likely noticed that when we were working with print( ) and echo( ) I made use of both single and double quotation marks.  In many settings there isn't really much to distinguish between their use characteristics. 

While this isn't likely to mean too much at present, things between two single quotation marks are treated literally.  Things between double quotation marks will be interpolated.  For example, assume we have the following two code snippets.

$var = 'test';
echo "var is equal to $var";

will result in

var is equal to test

The code snippet

$var = 'test';
echo 'var is equal to $var';

will result in

var is equal to $var

Another two examples of the same ilk are

$var = 'test';
echo "\$var is equal to $var";

will result in

$var is equal to test

But the code snippet

$var = 'test';
echo '\$var is equal to $var';

will result in

\$var is equal to $var

These examples should illustrate that using " will cause the value of a variable to be displayed.

The following is a modification of our very_basic_variables.php which should now be called very_basic_quotes.php.

 

<html>
<head>
<title>Some Very Basic Work With Variables</title>
</head>

<body bgcolor="005544" text="d0d0d0">
<?php
// assigning the information and displaying it
// using both double and single quotation marks

$first_number = 1.234;
print "<P>Using \" the first variable \$first_number has value <b>$first_number</b><BR>";
print 'Using \' the first variable \$first_number has value <b>$first_number</b></P>';
$second_number = 37.33;
echo "<P>Using \" the second variable \$second_number has value <b>$second_number</b><BR>";
echo 'Using \' the second variable \$second_number has value <b>$second_number</b></P>';
$product = $first_number * $second_number;
print "<P>Using \" the product of these two numbers \$product is <b>$product </b><BR>";
print 'Using \' the product of these two numbers \$product is <b>$product </b></P>';

?>
</body>
</html>

 

After uploading and running this in your account on the server you should see something like the following.  Though this presents only the very beginning of the results.

 

 

We'll look this over in class and discuss a few of the reported settings.