I am using xampp server to learn php and mysql, and I noticed that (\n) for a new line doesn't work?
Does anyone noticed this before in xampp?
This applies to \t,\r too.
From the PHP Manual:
The simplest way to specify a string is to enclose it in single quotes
(the character ').
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.
So you have to use double quotes to get \n, \t and other escaped characters (you can find a list on the linked manual page).
Your problem is not specific to XAMPP and is a general PHP thing.
They work in xampp.
You need to use double quotes: "\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.
I am very new to CakePHP and not very familiar with regular expressions.
I need to use regex in CakePHP to check whether a string has a double quotes character, followed immediately by a comma, then followed by another double quotes character: ","
Here is my attempt:
String::tokenize($problem_string, '/",/"');
I tried ($problem_string, ","), but that parsed the string at every place there was a comma. I also tried ($problem_string, "/",/""), with no luck.
This entry suggests using a backslash in front of the double quotes in Java, but maybe that rule doesn't apply for PHP or CakePHP?
How to represent the double quotes character (") in regex?
I feel like this should be an easy problem to figure out, but I've been stumped for quite a while now.
The escape character you're looking for is the backslash not the forward slash, but you don't have to escape double quotes if you use single quote delimiters, so just this: ($problem_string, '/","/')
Update
After reading String::tokenize docs, and not seeing any mention of regex, I think you just want ($problem_string, '","')
Both the following lines of calling name spaced method work, are there any case which single slash does not work?
<?php
namespace Foo\Bar;
class Dummy {
public static function hello() {
echo 'world';
}
}
echo \Foo\Bar\Dummy::hello();
call_user_func('\Foo\Bar\Dummy::hello');
call_user_func('\\Foo\\Bar\\Dummy::hello');
The reason I ask is: If single slash always work, why I see so many of double slash in the Internet, even the composer generate file like this [1]? Are there any case I am missing?
[1] https://github.com/ircmaxell/quality-checker/blob/master/vendor/composer/autoload_namespaces.php
Any literal notation works as is with a single backslash, that's how the syntax is defined:
namespace Foo\Bar;
echo \Foo\Bar\Dummy::hello();
When using strings, string escaping rules apply:
call_user_func('Foo\Bar\Dummy::hello');
call_user_func('Foo\\Bar\\Dummy::hello');
(BTW, don't start with a backslash in these cases, fully qualified class names in strings are always absolute, you don't need the starting backslash to resolve relative namespace references.)
In single quoted strings, only a single character needs to be escaped: '. I.e. if you want to write a single quote in single quotes, you need to escape it with a backslash:
echo 'don\'t';
That makes the backslash a special character, which you may need to escape as well:
echo 'backslash: \\';
So, in single quoted strings, \\ is always a single backslash. If you're using a single backslash and the next character is not a ' or \, then that single backslash is also just a single backslash:
echo 'just \ a \ backslash';
So except for those two cases, it makes no difference.
Double quoted strings have a lot more escape sequences like \n, which you'd need to take care of.
BTW, that's why many people were pretty unhappy with the choice of \ as a namespace separator, because it's already a character with a special meaning and leads to confusion and possibly bugs.
Within single quoted strings you only really need to use a backslash to escape a single quote or a backslash if it appears as the last character, e.g. 'foo\\'.
In your example, this line highlights this exact case:
'Symfony\\Component\\Routing\\' => $vendorDir . '/symfony/routing/',
^^
This incidentally is also the output of var_export():
$a = ['foo\bar\baz\\' => 'foo'];
var_export($a);
array (
'foo\\bar\\baz\\' => 'foo',
)
So even though it's technically not necessary to escape the other backslashes in the above example, var_export() will do this anyway.
See also: Strings and var_export()
In PHP, special characters don't need to be escaped when using single quotes [' ']. Most programming languages check things for \t (tab), \n (new line), \r (carriage return), and others when using double quotes [" "]. When you use double quotes in PHP, it searches for these, so you have to escape the special backslash character so PHP knows you're not escaping the character after the backslash. This is not the case with single quotes.
You can test this yourself:
echo "this\tis\ta\ttest";
echo 'this\tis\ta\ttest';
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