在php中转义引号有什么意义
这是我正在学习的一本书中的一个验证脚本,为什么要避开引号?e、 g.在php中转义引号有什么意义,php,Php,这是我正在学习的一本书中的一个验证脚本,为什么要避开引号?e、 g.char 创建数据库表:步骤2 用于 当您以“开头字符串时,任何未转换的”将标记字符串的结尾: $str = " A string can be enclosed in ' or "."; ^ this " which is not escaped will prematurely end the string. $form_block .=
char
创建数据库表:步骤2
用于
当您以“
开头字符串时,任何未转换的”
将标记字符串的结尾:
$str = " A string can be enclosed in ' or ".";
^
this " which is not escaped will prematurely end 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>';
my_string = 'hello "world"';
为了避免这种情况,我们对出现在“
类似地,包含在“”中的字符串中的“unscaped”也会导致问题
一个简单的解决方法是认识到“
内部”
是按字面意思处理的,因此“
内部”
因此,如果要避免转义
“
您可以将字符串括在”
中,但在”
中,变量将不会被插入,您需要记住这一点。这是必要的,因为您要将值赋给字符串。您也可以使用单引号,而不必转义双引号,但必须小心转义字符串中的任何单引号:
$str = " A string can be enclosed in ' or ".";
^
this " which is not escaped will prematurely end 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>';
my_string = 'hello "world"';
$form_block.='
烧焦
日期
浮动
int
文本
瓦尔查尔
';
对字符串中的引号进行转义可以防止它们结束字符串。例如:
$str = "Hi this is a "broken" string";
my_string = "hello "world"";
<?php
for ($i = 0; $i < 10; $i++)
{
echo <<<SOMELABEL
Text number $i:
<input type="text" name="textfield[$i]" />
<br />
SOMELABEL;
}
?>
基本上,PHP解析器可以看到多个语句:“Hi这是一个”
,断开的
,以及“字符串”
。它将成为无效的代码行
当解析器遇到第一个引号时,它知道它找到了一个字符串,并且知道下一个引号告诉它字符串的结束位置。如果希望在字符串中包含引号,则需要通过在前面使用反斜杠转义引号来告诉解析器它们不是字符串的结尾
如果字符串以单引号开始,”
,则只需在字符串中转义单引号。双引号也一样。这两行都是有效代码:
$str = "This string is 'not' broken";
$str = 'This string is also "not" broken';
您只需观察用于打开和关闭字符串的字符。如果您在字符串中引用带有
“
”的字符串,然后在字符串中使用该字符,它将结束字符串。相反,请同时使用”
和“
'char'
您可以使用它们告诉PHP引号是字符串的一部分,而不是字符串的结尾。例如:
$str = "Hi this is a "broken" string";
my_string = "hello "world"";
<?php
for ($i = 0; $i < 10; $i++)
{
echo <<<SOMELABEL
Text number $i:
<input type="text" name="textfield[$i]" />
<br />
SOMELABEL;
}
?>
如果出现错误,PHP解析器就会感到困惑。您可以选择以下两种方式:
my_string = "hello \"world\"";
或使用单引号分隔字符串:
$str = " A string can be enclosed in ' or ".";
^
this " which is not escaped will prematurely end 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>';
my_string = 'hello "world"';
$form_block=”
转义引号是必要的,因为PHP解析器会假定这些引号以字符串结尾,而不是字符串的一部分
为了避免转义引号,在某些情况下可以使用撇号,但撇号内包含的字符串中的变量不会被解析(即,变量的内容不会被输出,只输出变量的名称)
但是,在一次输出多行HTML时,单引号和双引号都不太合适。也许这本书不想让读者感到困惑或引入新概念,但一般来说,如果您有一个长HTML块,希望不带变量输出,那么最好简单地退出PHP模式,如:
<?php
for ($i = 0; $i < 10; $i++)
{
?>
Some text goes here
<input type="text" name="textfield[]" />
<br />
<?php
}
?>
这里有一些文字
这在计算上都是最优的,并且允许您编写文本,而不必担心转义序列或类似的问题。但是,如果您需要变量,它可能并不总是最佳解决方案(不过,您可以始终返回PHP模式以输出变量).出于这些目的,字符串有一个heredoc语法。例如:
$str = "Hi this is a "broken" string";
my_string = "hello "world"";
<?php
for ($i = 0; $i < 10; $i++)
{
echo <<<SOMELABEL
Text number $i:
<input type="text" name="textfield[$i]" />
<br />
SOMELABEL;
}
?>
该字符串由从开始的所有文本组成。您还可以使用以下方法绕过转义引号
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;
echo