php singlequote无法正常使用sprintf

时间:2012-05-16 09:48:32

标签: php string

我在程序中使用sprintf()输出一些标签和换行符。我注意到我的程序中有一部分工作不正常。

当我检查不起作用的部分时,我注意到我使用单引号'而不是双引号",程序实际输出\t而不是不可见的标签空间。

我认为这两者是相似的,并且为php提供两个分隔符的原因是我们能够在字符串中插入单引号或双引号或者echo而不插入转义字符。

除了我发现的变量之外,分配变量会有区别吗

$a = "qq";
$b = 'qq';

它们会以不同的方式存储在计算机的内存中吗?

3 个答案:

答案 0 :(得分:1)

你可以参考手册,它指明php中的单引号将大多数转义序列视为litterals,而不是双引号: http://php.net/manual/en/language.types.string.php

答案 1 :(得分:1)

  • 单引号快于双
  • 双引号可以解析php变量。即 $ a = 2; 如果您使用 echo“a是:$ a”; 那么它将打印 a is:2 但是单个报价将打印 a is:$ a

答案 2 :(得分:0)

if you use single quotes for the format string (like you should do, since there 
aren't any variable conversions to do as long as you don't need any special chars), 
the given examples won't work because of the backslash before the $ (needs to be 
escaped in double quoted strings - but not in single quoted!) http://php.net/manual/en/function.sprintf.php