这是我正在学习的书中的验证脚本,为什么要删除引号necassery?例如<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>
Run Code Online (Sandbox Code Playgroud)
转义字符串中的引号可防止它们结束字符串.例如:
$str = "Hi this is a "broken" string";
Run Code Online (Sandbox Code Playgroud)
本质上,PHP解析器看到多个statments: ,"Hi this is a"
,broken
和"string "
.它变成了无效的代码行.
当解析器遇到第一个引号时,它知道它找到了一个字符串,它知道下一个引号告诉它字符串结束的位置.如果要在字符串中包含引号,则需要告诉解析器它们不是字符串的结尾,方法是在前面使用反斜杠转义它们.
如果你用单引号开始你的字符串'
,那么你只需要在字符串中转义单引号.与双引号相同.这两行都是有效的代码:
$str = "This string is 'not' broken";
$str = 'This string is also "not" broken';
Run Code Online (Sandbox Code Playgroud)
您只需要注意用于打开和关闭字符串的任何一个.