I am confused with following string function
echo strlen("l\n2"); //give 3 in output
where as
echo strlen('l\n2'); //give 4 in output
can anybody explain why ?
Because when you use single quotes (' '), PHP does not expand the \n as a single new line character whereas in double quotes (" "), \n translates to the new line character (ie. a single character) thus giving 3 characters
Taken from PHP's String Documentation: http://php.net/manual/en/language.types.string.php
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
\n is not parsed as a newline character when the string is wrapped in single quotes. Instead, it is treated as a literal \ followed by n.
Related
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\ '
Whenever strings are set with single quotes the unicode doesn't get decoded but the unicode does get decoded when set with double quotes.
How do I get the strings set by single quotes also to be decoded?
PHP
$poo = '\u{1F6BB}';
echo $poo;
$poo = "\u{1F6BB}";
echo $poo;
OUTPUT
\u{1F6BB}🚻
Example
http://sandbox.onlinephpfunctions.com/code/9a38e972226a6271996f512363c19332dae0b760
The point of single-quoted strings is that they don't support escape characters.
The documentation says this very clearly:
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.
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
//remove line breaks
function safeEmail($string) {
return preg_replace( '((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' , '', $string );
}
/*** example usage 1***/
$from = 'HTML Email\r\t\n';
/*** example usage 2***/
$from = "HTML Email\r\t\n";
if(strlen($from) < 100)
{
$from = safeEmail($from);
echo $from;
}
1 returns HTML Email\r\t\n while
2 returns HTML Email
what's with the quotes?
As per the PHP Documentation
Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
In other words, double quoted strings expand variables and escape sequences for special characters. Single quoted strings don't.
So in example1, with the single quoted string, the string is exactly as you see it. Slashes and all.
But in example2, rather than ending with the string \r\t\n, it ends with a carriage return, a tab and then a new line. In other words the escape sequences for special characters are expanded.
with single quotes in PHP those special characters as \n \r \t... doesn't work as expected.
According to the docs:
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.
<?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.