MegaNairda
MegaNairda

Reputation: 839

php singlequote not working on sprintf properly

I use sprintf() on my program to output some tabs and newlines. I noticed a part of my program not working properly.

As I inspected the part that isn't working, I noticed that I used a single quote ' instead of a doublequote " and the program actually outputs a \t instead of a inivisible tab space.

I thought the two are similar and the reason for having two delimeters for php is for us to be able to insert single or doublequote in a string or echo them without inserting escape characters.

Would there be a difference in assigning variables aside from the one I discovered

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

Would they be stored in the computer's memory in a different manner?

Upvotes: 0

Views: 3346

Answers (3)

VibhaJ
VibhaJ

Reputation: 2256

  • single quote is faster than double
  • double quote can parse php variable. i.e. $a=2; and if you use echo "a is: $a"; then it will print a is: 2 but single quote will print a is: $a

Upvotes: 1

Suleman Ahmad
Suleman Ahmad

Reputation: 2103

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

Upvotes: 0

Gaet
Gaet

Reputation: 689

you can refer to the manual that specifies that single quotes in php consider most escape sequences as litterals, contrary ot double quotes: http://php.net/manual/en/language.types.string.php

Upvotes: 1

Related Questions