这是我正在学习的一本书中的验证脚本,为什么需要转义引号?例如<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>
对字符串内的引号进行转义可以防止它们结束字符串。例如:
$str = "Hi this is a "broken" string";
本质上,PHP 解析器会看到多个语句:"Hi this is a"
, broken
, and "string "
。它成为无效的代码行。
当解析器遇到第一个引号时,它知道它找到了一个字符串,并且它知道下一个引号告诉它字符串结束的位置。如果你想加引号inside您的字符串,您需要通过在前面使用反斜杠转义它们来告诉解析器它们不是字符串的结尾。
如果您的字符串以单引号开头,'
,那么您只需要在字符串中转义单引号即可。与双引号相同。这两行都是有效的代码:
$str = "This string is 'not' broken";
$str = 'This string is also "not" broken';
你只需要注意你用来打开和关闭琴弦的那一个即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)