Переменные и константы в php

Anonymous functions

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of parameters, but they have many other uses.

Anonymous functions are implemented using the Closure class.

Example #1 Anonymous function example


Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon:

Example #2 Anonymous function variable assignment example

Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct. From PHP 7.1, these variables must not include superglobals, $this, or variables with the same name as a parameter.

Example #3 Inheriting variables from the parent scope

The above example will output something similar to:

Notice: Undefined variable: message in /example.php on line 6
NULL
string(5) "hello"
string(5) "hello"
string(5) "hello"
string(5) "world"
string(11) "hello world"

Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from). See the following example:


Example #4 Closures and scoping

Example #5 Automatic binding of $this

The above example will output:

object(Test)#1 (0) {
}

Output of the above example in PHP 5.3:

Notice: Undefined variable: this in script.php on line 8
NULL

As of PHP 5.4.0, when declared in the context of a class, the current class is automatically bound to it, making $this available inside of the function’s scope. If this automatic binding of the current class is not wanted, then may be used instead.

Static anonymous functions

As of PHP 5.4, anonymous functions may be declared statically. This prevents them from having the current class automatically bound to them. Objects may also not be bound to them at runtime.

Example #6 Attempting to use $this inside a static anonymous function

The above example will output:

Notice: Undefined variable: this in %s on line %d
NULL

Example #7 Attempting to bind an object to a static anonymous function

The above example will output:

Warning: Cannot bind an instance to a static closure in %s on line %d

Predefined Constants

Core Predefined Constants

These constants are defined by the PHP core. This includes PHP, the Zend engine, and SAPI modules.

(string)

The current PHP version as a string in «major.minor.release» notation.

(integer)

The current PHP «major» version as an integer (e.g., int(5) from version «5.2.7-extra»). Available since PHP 5.2.7.

(integer)

The current PHP «minor» version as an integer (e.g., int(2) from version «5.2.7-extra»). Available since PHP 5.2.7.

(integer)

The current PHP «release» version as an integer (e.g., int(7) from version «5.2.7-extra»). Available since PHP 5.2.7.

(integer)

The current PHP version as an integer, useful for version comparisons (e.g., int(50207) from version «5.2.7-extra»). Available since PHP 5.2.7.

(string)

The current PHP «extra» version as a string (e.g., ‘-extra’ from version «5.2.7-extra»). Often used by distribution vendors to indicate a package version. Available since PHP 5.2.7.

(integer)

Available since PHP 5.2.7.

(integer)

Available since PHP 5.2.7.

(integer)

The maximum length of filenames (including path) supported by this build of PHP. Available since PHP 5.3.0.

(string)

The operating system PHP was built for.

(string)

The operating system family PHP was built for. One of ‘Windows’, ‘BSD’, ‘Darwin’, ‘Solaris’, ‘Linux’ or ‘Unknown’. Available as of PHP 7.2.0.

(string)

The Server API for this build of PHP. See also php_sapi_name().

(string)

The correct ‘End Of Line’ symbol for this platform. Available since PHP 5.0.2

(integer)

The largest integer supported in this build of PHP. Usually int(2147483647) in 32 bit systems and int(9223372036854775807) in 64 bit systems. Available since PHP 5.0.5

(integer)

The smallest integer supported in this build of PHP. Usually int(-2147483648) in 32 bit systems and int(-9223372036854775808) in 64 bit systems. Available since PHP 7.0.0. Usually, PHP_INT_MIN === ~PHP_INT_MAX.

(integer)

The size of an integer in bytes in this build of PHP. Available since PHP 5.0.5

(integer)

Number of decimal digits that can be rounded into a float and back without precision loss. Available as of PHP 7.2.0.

(float)

Smallest representable positive number x, so that x + 1.0 != 1.0. Available as of PHP 7.2.0.

(float)

Smallest representable positive floating point number. If you need the smallest representable negative floating point number, use — PHP_FLOAT_MAX. Available as of PHP 7.2.0.

(float)

Largest representable floating point number. Available as of PHP 7.2.0.

(string)
(string)
(string)
(string)
(string)

The value «—prefix» was set to at configure.

(string)

Specifies where the binaries were installed into.

(string)

Specifies the PHP binary path during script execution. Available since PHP 5.4.

(string)

Specifies where the manpages were installed into. Available since PHP 5.3.7.

(string)
(string)
(string)
(string)
(string)
(string)
(string)

The build-platform’s shared library suffix, such as «so» (most Unixes) or «dll» (Windows).

(string)

The maximum number of file descriptors for select system calls. Available as of PHP 7.1.0.

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Error reporting constant. Available since PHP 5.2.0

(integer)

Error reporting constant. Available since PHP 5.3.0

(integer)

Error reporting constant. Available since PHP 5.3.0

(integer)

Error reporting constant

(integer)

Error reporting constant

(integer)

Available since PHP 5.1.0

(boolean)

See Booleans.

(boolean)

See Booleans.

(null)

See Null.

(integer)

A Windows CTRL+C event. Available as of PHP 7.4.0 (Windows only).

(integer)

A Windows CTRL+BREAK event. Available as of PHP 7.4.0 (Windows only).

See also: Magic constants.


С этим читают