tags:

views:

177

answers:

9

Here is a validation script from a book I am learning, Why is escaping the quotation marks necassery? e.g. <option value=\"char\">char</option>

<?php
//validate important input
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
    header( "location: show_createtable.html");
           exit;
}

//begin creating form for display
$form_block = "
<form action=\"do_createtable.php\" method=\"post\">
<input name=\"table_name\" type=\"hidden\" value=\"$_POST[table_name]\">
<table cellspacing=\"5\" cellpadding=\"5\">
  <tr>
    <th>Field Name</th><th>Field Type</th><th>Table Length</th>
  </tr>";

//count from 0 until you reach the number fo fields
for ($i = 0; $i <$_POST[num_fields]; $i++) {
  $form_block .="
  <tr>
  <td align=center><input type=\"texr\" name=\"field name[]\"
  size=\"30\"></td>
  <td align=center>
    <select name=\"field_type[]\">
        <option value=\"char\">char</option>
        <option value=\"date\">date</option>
        <option value=\"float\">float</option>
        <option value=\"int\">int</option>
        <option value=\"text\">text</option>
        <option value=\"varchar\">varchar</option>
        </select>
  </td>
  <td align=center><input type=\"text\" name=\"field_length[]\" size=\"5\">
  </td>
</tr>";
}

//finish up the form 
$form_block .= "
<tr>
    <td align=center colspan=3><input type =\"submit\" value=\"create table\">
    </td>
</tr>
</table>
</form>";

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create a database table: Step 2</title>
</head>

<body>
<h1>defnie fields for <? echo "$_POST[table_name]"; ?> 
</h1>
<? echo "$form_block"; ?>

</body>
</html>
+2  A: 

When you start a string with ", any unescaped " inside will mark the end of the string:

$str = " A string can be enclosed in ' or ".";
                                          ^
this " which is not escaped will prematurely end the string.

To avoid this we escape any " appearing within ""

$str = " A string can be enclosed in ' or \".";

Similarly a unescaped ' in a string enclosed in ' ' will cause problems.

A simple work around is to realize that a " inside ' ' is treated literally and so a ' inside " "

So if you want to avoid escaping " you can enclose the string in ' ' but inside a ' ' variables will not be interpolated, you need to remember that.

codaddict
+2  A: 

It's necessary there because you're assigning the value to a string. You could also use single quotes and not have to escape double quotes, but you'll have to be careful and escape any single quotes in the string:

$form_block .= '
<tr>
<td align=center><input type="texr" name="field name[]"
size="30"></td>
<td align=center>
  <select name="field_type[]">
      <option value="char">char</option>
      <option value="date">date</option>
      <option value="float">float</option>
      <option value="int">int</option>
      <option value="text">text</option>
      <option value="varchar">varchar</option>
      </select>
</td>
<td align=center><input type="text" name="field_length[]" size="5">
</td>
</tr>';
rspeicher
+6  A: 

Escaping the quotation marks inside a string prevents them from ending the string. For example:

$str = "Hi this is a "broken" string";

Essentially the PHP parser sees multiple statments: "Hi this is a", broken, and "string ". It becomes an invalid line of code.

When the parser encounters the first quotation mark, it knows it's found a string, and it knows that the next quotation mark tells it where the string ends. If you want to have quotation marks inside your string, you need to tell the parser that they aren't the end of the string by escaping them with backslashes in front.

If you start your string with single quotes, ', then you only need to escape single quotes inside your string. Same with double quotes. These two lines are both valid code:

$str = "This string is 'not' broken";
$str = 'This string is also "not" broken';

You just have to watch for whichever one you used to open and close the string.

zombat
+1  A: 

If you quote a string with " and then use that character inside the string, it will end the string. Instead, use both ' and "

'<option value="char">char</option>'

adam
+1  A: 

You use them to tell PHP that the quotation marks are part of a string and not the end of it. For example:

my_string = "hello "world"";

would give an error, the PHP parser gets confused. Your options are to either escape them:

my_string = "hello \"world\"";

or use single quotes to delimit the string:

my_string = 'hello "world"';
Rodin
+1  A: 
$form_block = "
<form action=\"do_createtable.php\"...

The string stored in, e.g., form_block is enclosed in quotation marks to denote that it is a string.

Now that string contains the quotation mark character within the string, but if you are to represent that quotation mark with simply a ", without escaping, then the php interpreter will understand that as the end of the string.

E.g.

$form_block = "
<form action=\"do_createtable.php\"
//            ^
//            |
//          escaped quotation mark, part of string.
//      notice how this text is red because it's a string.
//      and I need to end this line with a quotation mark to end the string."
$form_block = "
<form action="do_createtable.php\"
//           ^
//           |
//          not escaped quotation mark, so string will end there, it will be "<form action="
ladaghini
+2  A: 

Escaping the quotation marks is necessary, because the PHP parser would otherwise assume that those quotation marks end the string instead of being part of the string.

To avoid escaping the quotation marks, you may in some cases use apostrophes instead, but variables inside strings enclosed within apostrophes wont be parsed (i.e. the contents of the variable wont be outputted, only the name of the variable).

However, neither single nor double quotes are that favorable when outputting multiple lines of HTML in one go. Perhaps the book did not want to confuse the reader or introduce new concepts yet, but in general, if you have a long HTML block you want to output without variables, then it's best to simply exit the PHP mode, like:

<?php
for ($i = 0; $i < 10; $i++)
{
?>
Some text goes here
<input type="text" name="textfield[]" />
<br />
<?php
}
?>

This is optimal both computationally and allows you to write the text without worrying about escape sequences or anything like that. However, if you need to have variables, it may not be always the best solution (though, you could always just go back into PHP mode to output the variable). For these purposes, there's the heredoc syntax for strings. For example:

<?php
for ($i = 0; $i < 10; $i++)
{
    echo <<<SOMELABEL
Text number $i: 
<input type="text" name="textfield[$i]" />
<br />
SOMELABEL;
}
?>

The string consists of all the text that begins from the <<<SOMELABEL until the line that only contains the text SOMELABEL; (obviously, you can name the label anything you want). All the variables and PHP escape sequence work within the heredoc syntax, so it can be ideal, when the string consists of multiple lines of HTML, because you don't need to worry about escaping any quotes.

Rithiur
A: 

8 answers and not a single manual link. That's scaring me.

Col. Shrapnel
+1  A: 

you can also bypass escaping quotes at all using something like

echo <<<EOD
    <td align=center><input type="texr" name="field name[]"
    size="30"></td>
    <td align=center>
      <select name="field_type[]">
          <option value="char">char</option>
          <option value="date">date</option>
          <option value="float">float</option>
          <option value="int">int</option>
          <option value="text">text</option>
          <option value="varchar">varchar</option>
          </select>
EOD;

where echo <<<EOD; and EOD; are on their own lines

Jamie