这是在字符串中传递单引号和双引号的正确方法

时间:2011-01-25 15:30:53

标签: php

'<a href="javascript:void(0)" onClick="function(\''.$row['STUID'].'\')">Print STUDENT DETAILS</a>';

这是在语句

中传递单引号和双引号的正确方法吗?

2 个答案:

答案 0 :(得分:2)

是的,确实如此。首先,您转义传递给输出的单引号,并引用函数参数。在它们内部,你有引号将你的字符串与php变量分开。

你不必转义php字符串中的双引号,因为它是单引号分隔的。

$string = "this is a 'valid' string.";
$string = 'this is a \'valid\' string.';
$string = 'this is a "valid" string.';
$string = "this is a \"valid\" string.";
$valid = "valid";
$string = "this is a '".$valid."' string.";
$string = 'this is a \''.$valid.'\' string.';
$string = 'this is a "'.$valid.'" string.';
$string = "this is a \"".$valid."\" string.";
$string = "this is a '$valid' string"; //as Rocket said
$string = 'this is NOT a "$valid" string'; //Works only in double quotes
$string = "this is a '{$row['valid']}' string"; //as Rocket said

答案 1 :(得分:0)

是肯定的。 单引号和双引号之间的区别在于php将尝试解释双引号内的特殊字符和变量。但变量解释不适用于数组。

此处有更多信息: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double