Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can null be written with a backslash?

In all PHP manuals, it says that null, true, and false are internal values. However, nowhere does it say why they can be written with backslash: \null, \false, \true.

What exactly are null, false, and true in PHP?

A constant or are they some hack for the interpreter?

like image 511
Kart Av1k Avatar asked Jan 01 '23 01:01

Kart Av1k


1 Answers

null, false and true are defined in PHP as constants. The backslash means global namespace, so you can just specify the full namespace for these constants like this: \null. This is not necessary, because all constants are global (apart from the ones defined using const).

Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope. For more information on scope, read the manual section on variable scope. - PHP Manual on Constants

Functions and constants do not need to have fully qualified name. This is in contrast to classes which must be specified with their namespace e.g. \Exception.

For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist. - PHP Manual on namespace fallback

EDIT

null, false and true are very special constants in PHP. They are the only ones which are still explicitly "case-insensitive". The reason why I put it in quotes is because the actual constant name is defined as NULL, FALSE, TRUE, except... it actually isn't. It is lower-cased:

define('TRUE', 'foo'); // works
define('True', 'bar'); // works too, even in combination with the first one
define('true', 'abc'); // Notice: Constant true already defined

var_dump(constant('TRUE')); // string 'foo'
var_dump(constant('True')); // string 'bar'
var_dump(constant('true')); // boolean true

If you remove the define()-s then all constants will point back to the boolean even if you use constant('True'). - https://ideone.com/CZSwiX

PHP manual states:

To specify a boolean literal, use the constants TRUE or FALSE. Both are case-insensitive.

You might ask yourself then, what is the value of these constants? If you execute var_dump(TRUE); the result will be bool(true), and if you do echo true; it will print out 1. What are they defined as in the PHP source code? Here is the answer:

REGISTER_MAIN_BOOL_CONSTANT("TRUE", 1, CONST_PERSISTENT);
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

What is more interesting is that they are really treated as special constants and cannot be redefined by the user (other predefined constants can be redefined in their own namespace with const). I would therefore argue that for all intents and purposes you can treat these 3 as not just constants, but special values akin to reserved keywords. For the moment they are still implemented as a case-insensitive hack in PHP, but as the comment in the commit by @NikiC states: "In the future we may convert them from constants to reserved keywords."

Interesting fact: Until PHP 7 true, false and null were not reserved keywords, so you could try something like this:

class TRUE {
    const int = 'This is the truest of the integers';
}
var_dump(TRUE::int);

tl;dr:
Accessing the \null, \false and \true works, because they are internally defined as special-constants and all constants in PHP are in the global namespace.

like image 181
Dharman Avatar answered Jan 08 '23 02:01

Dharman