242

What does a \ do in PHP?

For example, CSRF4PHP has \FALSE, \session_id, and \Exception:

public function __construct($timeout=300, $acceptGet=\FALSE){
    $this->timeout = $timeout;
    if (\session_id()) {
        $this->acceptGet = (bool) $acceptGet;
    } else {
        throw new \Exception('Could not find session id', 1);
    }
}
Webber
  • 4,672
  • 4
  • 29
  • 38
Alfred
  • 60,935
  • 33
  • 147
  • 186
  • 1
    @mario very nice stackoverflow topic. Thanks for the link :) – Alfred Jan 25 '11 at 04:39
  • I guess this question should be closed, but I can't close topics myself.. – Alfred Jan 25 '11 at 04:40
  • 5
    It's a duplicate, but not a bad question. But you could add "backslash" to the title, so it becomes easier to google for someone else. – mario Jan 25 '11 at 04:43
  • 1
    It is important to realize, they don't shoot the messenger anymore. When you find out what it is, don't blame us :) OH NO, I'm TOO LATE. – Tim Post Jan 25 '11 at 04:46
  • If this is duplicated please link the duplicated item. – MaXi32 Jan 06 '22 at 14:08
  • Exactly what I was wondering, so I googled and found this question. SO works as it should. – klutt Aug 10 '22 at 07:31

4 Answers4

334

\ (backslash) is the namespace separator in PHP 5.3.

A \ before the beginning of a function represents the Global Namespace.

Putting it there will ensure that the function called is from the global namespace, even if there is a function by the same name in the current namespace.

felipe.zkn
  • 2,012
  • 7
  • 31
  • 63
Alan Geleynse
  • 24,821
  • 5
  • 46
  • 55
38

Namespaces

In PHP 5.3+ the backslash \ symbol is used in namespaces. It is the start symbol to indicate a namespace and also serves as a separator between sub-namespace names.

See official documentation about namespacing.

Opcache

Additionally in PHP 7.0+ some functions are replaced with opcodes by OPCache, which makes these specific functions run a lot faster. However this only works when the functions are placed in the root namespace. See this discussion about this topic. So besides namespacing, the \ indirectly also affects code optimisation.

The following native functions benefit from this effect:

"array_slice"
"assert"
"boolval"
"call_user_func"
"call_user_func_array"
"chr"
"count"
"defined"
"doubleval"
"floatval"
"func_get_args"
"func_num_args"
"get_called_class"
"get_class"
"gettype"
"in_array"
"intval"
"is_array"
"is_bool"
"is_double"
"is_float"
"is_int"
"is_integer"
"is_long"
"is_null"
"is_object"
"is_real"
"is_resource"
"is_string"
"ord"
"strlen"
"strval"
Webber
  • 4,672
  • 4
  • 29
  • 38
  • 3
    If it cause codes run faster wondering why wouldn't they apply it by default ? who doesnt like performance? – Mwthreex Jan 31 '21 at 13:24
  • Probably because OPCache wasn't configured by default for a long time, and the feature wasn't even included until version 7. It also requires knowledge to set up and [configure](https://www.php.net/manual/en/opcache.installation.php). – Webber May 02 '21 at 13:02
32

To clarify potential confusion:

The backslash does not imply class inheritance.

In the following, Animal, Dog, Shepherd don't have to be classes, but simply namespaces. Meaning something used to group names together to avoid naming collisions.

$myDog = new \Animal\Dog\Shepherd\GermanShepherd();

The leading \ means Animal was declared in the global scope.

Community
  • 1
  • 1
Nelu
  • 16,644
  • 10
  • 80
  • 88
10

The \ is used in PHP 5.3 for namespaces. See http://www.php.net/manual/en/language.namespaces.rationale.php for more information on namespaces and PHP.

Damian Galarza
  • 224
  • 2
  • 7