What is the difference between these two code snippet. First one with \ and second one with \\.
First One:
<?php
// File: app/autoload.php
$loader->registerNamespaces(array(
'Knp\\Component' => __DIR__.'/../vendor/knp-components/src',
'Knp\\Bundle' => __DIR__.'/../vendor/bundles',
// ...
));
And second one:
<?php
// File: app/autoload.php
$loader->registerNamespaces(array(
'Knp\Component' => __DIR__.'/../vendor/knp-components/src',
'Knp\Bundle' => __DIR__.'/../vendor/bundles',
// ...
));
Are they different or do they work the same?
The backslash has a special meaning in double quoted strings: it's used for escaping various characters (such as \n and \r).
In single quoted strings the backslash is used for escaping the literal quote (eg echo 'I\'m';) and the backslash itself.
It is better practice to use double backslashes in namespace strings to prevent any possible errors due to character escaping. Other than that, they are the same thing:
// outputs: Foo\Bar\Baz
echo 'Foo\Bar\Baz';
// outputs: Foo\Bar\Baz
echo 'Foo\\Bar\\Baz';
// The autoloader would not be able to find the correct file
// outputs: Foo
// ot hat
echo "Foo\not\that";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With