1
<?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

4
  • Can we see some more of your code. The backslashes don't matter. You should be seeing style=\"margin-right:0px\" not style="" even with the slashes. Commented Nov 28, 2010 at 4:27
  • like do you by chance have style="<?php echo ($i % 6 == 5) ? 'style=\"margin-right:0px\"' : ''; ?>" Commented Nov 28, 2010 at 4:27
  • Where are you looking at the result? Raw output? Browser? Browser's DOM inspector? Commented Nov 28, 2010 at 4:31
  • @All - I see so many good comments and explanations. Thanks a lot to all really. K. Regards. Commented Nov 28, 2010 at 14:11

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Right, so it outputs style=\"margin-right:0px\" not style="", so this isn't his problem.
@Typeoneerror, yes, this does not address his first question [well, actually there is no first question], but only his second question he asked on his update.
If he's looking at it in a browser (even via "view source"), I suspect that the browser's xml/html parser is filtering and discarding the illegal attribute value in style=\"margin-right:0px\", leaving only style="".
@Lee - Yes that was the case.
mark this one as an answer to the fact that it has @lee comments that, with that, answers both questions. I wish I could mark 3 as answers. :) (stackoverflow has not taken lessons from history so it stays with monotheism, but no one explains why monotheism is better). Oh well. Cheers. :D
2

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.

1 Comment

somebody downvoted my somewhat similar answer as well. Not sure why. After your edit, I feel like your answer is a pretty good one. +1, so at least you're even now. Explanations would be nice.
2

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.

1 Comment

@nhinkie - thanks a lot. That was a very clear explanation that I missed for long time. Cheers! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.