Question

<?php echo ($i % 6 == 5) ? 'style=\"margin-right:0px\"' : ''; ?>

I just get style="" printed on the view port.

Update: Why is unnecessary to escape double quotes when we are inside a string? Because double quotes will never be taken as anything else then a string, if they are inside single quotes?

Thanks in advance, MEM

Was it helpful?

Solution

Why is unnecessary to escape double quotes when we are inside a string? Because double quotes will never be taken as anything else then a string, if they are inside single quotes?

The manual, regarding single quoted strings:

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

OTHER TIPS

There's no need to escape double quotes within single quotes.

<?php echo ($i % 6 == 5) ? 'style="margin-right:0px"' : ''; ?>

You only need to escape single quotes within single quotes or double quotes within double quotes. If you want to write a single quote within a single quoted string, that single quote would terminate the string.

$foo = 'a'b';

PHP sees this as the string a, followed by a meaningless b and the start of the string '; which is never terminated; which is invalid syntax.

$foo = 'a\'b';

This is correctly parsed as the string a'b. You have escaped the meaning the quote would usually have at this point.

With double quotes within single quotes, there's no such ambiguity. A double quote within a single quoted string does not terminate the string, it has no such special meaning there that would need escaping. If you include a backslash, the backslash is used literally.

$foo = 'a"b';  // a"b
$foo = 'a\"b'; // a\"b

I suppose the problem is how you look at the output. If the output is style=\"…\", the escaped double quotes might cause invalid syntax in the environment where you're looking at the output.

You used single-quotes ' for that string, so escaping double quotes " inside the string is unnecessary. Replace that with 'style="margin-right:0px"' and it should work just fine.

To explain how PHP handles strings and quotes a bit better, it's helpful to know the difference between ' and ". Strings encapsulated with ' are always shown as-is. Nothing inside the string is parsed, including any escape characters (like \n for a newline or escaped quotes, except for escaped single quotes \'). Conversely, strings encapsulated in " are parsed, so if you have any escape characters they will be displayed properly, and if you have any variables within the string, they will be entered as well. For example,

// Set name variable to my name
$name = "nhinkle";

// Echo hello name with single quotes
echo 'hello {$name}';
// The result will be "hello {$name}"

// Echo hello name with double quotes
echo "hello {$name}";
// The result will be "hello nhinkle"

It takes less processing power to use single quotes, since PHP won't need to scan the string to escape anything, it just needs to find the end of the string. However, if you do need to parse things inside the string, make sure to use double quotes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top