2

I was wondering what would be the difference between Forward Slash "/" and Backslash "\" in php and Laravel. For example in every controller you have:

use Illuminate\Http\Request;

but if there was Forward slash instead of Backslash, it does not work:

use Illuminate/Http/Request;

and it throws an error:

"syntax error, unexpected '/', expecting ',' or ';'"

Armin
  • 2,021
  • 2
  • 19
  • 17
  • 3
    The difference is one is correct syntax, the other isn't. – Jonnix Jul 25 '19 at 20:20
  • 1
    @Jonnix yeah that's obvious, but is there something more to it than just syntax? – Armin Jul 25 '19 at 20:23
  • https://stackoverflow.com/questions/10788400/backslash-in-php-what-does-it-mean – aynber Jul 25 '19 at 20:24
  • I don't understand what you mean by "more than just syntax"? Example? – Jonnix Jul 25 '19 at 20:25
  • 2
    PHP, and most coding languages, have specific syntax, and symbols are not interchangeable. `/` is not the same as \, just as `//` is not the same as \\ – aynber Jul 25 '19 at 20:25
  • 2
    As pointed out, the namespace in PHP syntax requires the backslash separator. I just want to point out that this should not be confused with the path separator `DIRECTORY_SEPARATOR` which can be '/' or '\' depending on the operating system. PHP doesn't care if you use the '/' or '\' for these paths, regardless of operating system. – Alex Barker Jul 25 '19 at 20:38
  • @Jonnix I wasn't expecting it to be that simple, I taught there might be some deeper programming concepts involved or something. – Armin Jul 25 '19 at 20:45
  • Escape sequences, introduced by '\' in most languages. UNC share volumes in windows begin with two '\\'. URL uses two '//' after the colon. – Honk der Hase Jul 25 '19 at 21:05

1 Answers1

2

Namespaces are always separated by backslash. It's the defined separator.

You can read it here in the documentation of php: https://www.php.net/manual/en/language.namespaces.importing.php

Some more information you'll find in the FAQ for namespaces: https://www.php.net/manual/en/language.namespaces.faq.php

Marco
  • 3,470
  • 4
  • 23
  • 35