<?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
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.
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.
Related
I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.
PHP strings can be specified not just in two ways, but in four ways.
Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
Double quote strings will display a host of escape sequences (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are". Take a look at string parsing to see how to use array variables and such.
Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.
Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
Speed:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this
for($i=0;$i<100000;$i++) {
'string';
}
The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.
Things get evaluated in double quotes but not in single:
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
' Single quoted
The simplest way to specify a string is to enclose it in single quotes. Everything inside single quotes is treated as a plain string.
Example:
echo 'Start with a simple string';
echo 'Variables $var and escape sequences \n do not get interpolated';
echo 'You only need to escape the quote \' and a backslash itself\\';
" Double quoted
Strings in double quotes interpolate variables and a wide range of escape sequences. Note: Use curly braces {} to include complex variables or in case when next character after a variable is a valid character for a variable name.
Example:
echo "Start with a simple string";
echo "String's apostrophe";
echo "Escape sequences get interpolated\n";
echo "Variables $var get interpolated as well {$name}_sake";
Is there a performance benefit single quote vs double quote in PHP?
No.
In order to improve performance, PHP parses the entire code once and then stores the resulting bytecode in the opcode cache. This approach eliminates the entire parsing, along with whatever quoited strings as well.
A single-quoted string does not have variables within it interpreted. A double-quoted string does.
Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.
In PHP, both 'my name' and "my name" are string. You can read more about it at the PHP manual.
Thing you should know are
$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'
In PHP, people use single quote to define a constant string, like 'a', 'my name', 'abc xyz', while using double quote to define a string contain identifier like "a $b $c $d".
Both kinds of enclosed characters are strings. One type of quote is conveniently used to enclose the other type of quote. "'" and '"'. The biggest difference between the types of quotes is that enclosed identifier references are substituted for inside double quotes, but not inside single quotes.
Some might say that I'm a little off-topic, but here it is anyway:
You don't necessarily have to choose because of your string's content between:
echo "It's \"game\" time."; or echo 'It\'s "game" time.';
If you're familiar with the use of the english quotation marks, and the correct character for the apostrophe, you can use either double or single quotes, because it won't matter anymore:
echo "It’s “game” time."; and echo 'It’s “game” time.';
Of course you can also add variables if needed. Just don't forget that they get evaluated only when in double quotes!
I'm using PHP 7.1.11
As mentioned in the PHP manual
To specify a literal single quote in a string which is already
enclosed in a pair of single quotes, 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.
I'm not understanding the above paragraph due to which following doubts have been created in my mind :
Can only single quotes be specified in a string(using the escape sequence character \') already enclosed in single quotes?
Consider the below sentence
To specify a literal backslash, double it (\).
Actually, I can simply specify a single backslash literal by adding single \ in a string which is already enclosed in a pair of single quotes then why the manual is saying that I have to use double slash(\\) to specify it?
Now consider the below sentence
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.
Does this mean that no other escape sequence character except the single quote(\') can be added in a string quoted in single quotes? Will the characters like \r, \t and \n will get printed as it is like a simple text in a browser?
Someone please clarify all of my above doubts.
Thanks.
Regarding "first paragraph", it's about escaping backslash right before your escaped single quote
echo '\\\'' // Output: \'
All other backslashes output as it is, so there is no possibility to write "new line" symbol with single quotes.
echo '\n\ \'' // Output: \n\ '
I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.
PHP strings can be specified not just in two ways, but in four ways.
Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
Double quote strings will display a host of escape sequences (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are". Take a look at string parsing to see how to use array variables and such.
Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.
Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
Speed:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this
for($i=0;$i<100000;$i++) {
'string';
}
The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.
Things get evaluated in double quotes but not in single:
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
' Single quoted
The simplest way to specify a string is to enclose it in single quotes. Everything inside single quotes is treated as a plain string.
Example:
echo 'Start with a simple string';
echo 'Variables $var and escape sequences \n do not get interpolated';
echo 'You only need to escape the quote \' and a backslash itself\\';
" Double quoted
Strings in double quotes interpolate variables and a wide range of escape sequences. Note: Use curly braces {} to include complex variables or in case when next character after a variable is a valid character for a variable name.
Example:
echo "Start with a simple string";
echo "String's apostrophe";
echo "Escape sequences get interpolated\n";
echo "Variables $var get interpolated as well {$name}_sake";
Is there a performance benefit single quote vs double quote in PHP?
No.
In order to improve performance, PHP parses the entire code once and then stores the resulting bytecode in the opcode cache. This approach eliminates the entire parsing, along with whatever quoited strings as well.
A single-quoted string does not have variables within it interpreted. A double-quoted string does.
Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.
In PHP, both 'my name' and "my name" are string. You can read more about it at the PHP manual.
Thing you should know are
$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'
In PHP, people use single quote to define a constant string, like 'a', 'my name', 'abc xyz', while using double quote to define a string contain identifier like "a $b $c $d".
Both kinds of enclosed characters are strings. One type of quote is conveniently used to enclose the other type of quote. "'" and '"'. The biggest difference between the types of quotes is that enclosed identifier references are substituted for inside double quotes, but not inside single quotes.
Some might say that I'm a little off-topic, but here it is anyway:
You don't necessarily have to choose because of your string's content between:
echo "It's \"game\" time."; or echo 'It\'s "game" time.';
If you're familiar with the use of the english quotation marks, and the correct character for the apostrophe, you can use either double or single quotes, because it won't matter anymore:
echo "It’s “game” time."; and echo 'It’s “game” time.';
Of course you can also add variables if needed. Just don't forget that they get evaluated only when in double quotes!
I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.
PHP strings can be specified not just in two ways, but in four ways.
Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
Double quote strings will display a host of escape sequences (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are". Take a look at string parsing to see how to use array variables and such.
Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.
Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
Speed:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this
for($i=0;$i<100000;$i++) {
'string';
}
The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.
Things get evaluated in double quotes but not in single:
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
' Single quoted
The simplest way to specify a string is to enclose it in single quotes. Everything inside single quotes is treated as a plain string.
Example:
echo 'Start with a simple string';
echo 'Variables $var and escape sequences \n do not get interpolated';
echo 'You only need to escape the quote \' and a backslash itself\\';
" Double quoted
Strings in double quotes interpolate variables and a wide range of escape sequences. Note: Use curly braces {} to include complex variables or in case when next character after a variable is a valid character for a variable name.
Example:
echo "Start with a simple string";
echo "String's apostrophe";
echo "Escape sequences get interpolated\n";
echo "Variables $var get interpolated as well {$name}_sake";
Is there a performance benefit single quote vs double quote in PHP?
No.
In order to improve performance, PHP parses the entire code once and then stores the resulting bytecode in the opcode cache. This approach eliminates the entire parsing, along with whatever quoited strings as well.
A single-quoted string does not have variables within it interpreted. A double-quoted string does.
Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.
In PHP, both 'my name' and "my name" are string. You can read more about it at the PHP manual.
Thing you should know are
$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'
In PHP, people use single quote to define a constant string, like 'a', 'my name', 'abc xyz', while using double quote to define a string contain identifier like "a $b $c $d".
Both kinds of enclosed characters are strings. One type of quote is conveniently used to enclose the other type of quote. "'" and '"'. The biggest difference between the types of quotes is that enclosed identifier references are substituted for inside double quotes, but not inside single quotes.
Some might say that I'm a little off-topic, but here it is anyway:
You don't necessarily have to choose because of your string's content between:
echo "It's \"game\" time."; or echo 'It\'s "game" time.';
If you're familiar with the use of the english quotation marks, and the correct character for the apostrophe, you can use either double or single quotes, because it won't matter anymore:
echo "It’s “game” time."; and echo 'It’s “game” time.';
Of course you can also add variables if needed. Just don't forget that they get evaluated only when in double quotes!
Are there any reason why forward slash need to be printed via \\ even with single quoted string?
e.g.
php -r "print '\n';" # echo \n
Since single quotea string does not handle escape characters, so why the following statement is not valid?
php -r "print '\n\';"
Parse error: parse error in Command line code on line 1
Well, no variable substitution and no escaping is done for strings in single-quotes with the exception for escaped single quotes within a single quoted string to allow you to insert a single quote into your string without having to use a double-quoted string for that special case.
Therefore your example will properly escape the string-terminating single quote, resulting in an unterminated string, causing the parse error.
So to print a backslash in front of a single quote within a single-quoted string, you'll have to escape the backslash itself. The single quote and the backslash in front of a single quote are the only characters you have to escape within a single-quoted string. All other characters will not get their special meaning applied if you use an escape sequence.
For single quotes, \' represents a literal single quote. Thus, your string is never closed.
If you have a web server installed on your computer the script you want to run is
`
echo "\n";
?>`
in order to print a newline character