PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
PHP provides several magic constants and predefined constants that can be used in your scripts. Magic constants are special constants that change depending on where they are used. Predefined constants are constants that are always available.
Magic Constants
__LINE__
: The current line number of the file.__FILE__
: The full path and filename of the file. If used inside an include, the name of the included file is returned.__DIR__
: The directory of the file. If used inside an include, the directory of the included file is returned.__FUNCTION__
: The function name.__CLASS__
: The class name.__METHOD__
: The class method name.__NAMESPACE__
: The name of the current namespace.Here's an example of how you can use these magic constants:
namespace MyNamespace; class MyClass { public function myFunction() { echo __LINE__; // Outputs: 5 echo __FILE__; // Outputs: /full/path/to/file.php echo __DIR__; // Outputs: /full/path/to echo __FUNCTION__; // Outputs: myFunction echo __CLASS__; // Outputs: MyClass echo __METHOD__; // Outputs: MyClass::myFunction echo __NAMESPACE__; // Outputs: MyNamespace } }
Predefined Constants
There are many predefined constants, including but not limited to:
PHP_VERSION
: The current PHP version as a string.PHP_INT_MAX
: The maximum integer size.PHP_EOL
: The correct 'End Of Line' symbol for this platform.DIRECTORY_SEPARATOR
: The correct directory separator for this platform (either '/' or '').PATH_SEPARATOR
: The correct PATH separator for this platform (either ':' or ';').You can use these constants anywhere in your scripts:
echo PHP_VERSION; // Outputs: 7.4.0 (for example) echo PHP_INT_MAX; // Outputs: 9223372036854775807 (for example) echo PHP_EOL; // Outputs: \n (for example) echo DIRECTORY_SEPARATOR; // Outputs: / (for example) echo PATH_SEPARATOR; // Outputs: : (for example)
__LINE__
, __FILE__
, and __DIR__
in PHP:
echo __LINE__; // Output: Current line number echo __FILE__; // Output: Full path and filename of the file echo __DIR__; // Output: Directory of the file
__FUNCTION__
, __CLASS__
, and __METHOD__
usage in PHP:
function exampleFunction() { echo __FUNCTION__; // Output: exampleFunction echo __CLASS__; // Output: (empty string) as not in a class } class ExampleClass { public function exampleMethod() { echo __METHOD__; // Output: ExampleClass::exampleMethod echo __CLASS__; // Output: ExampleClass } }
__NAMESPACE__
and __TRAIT__
in PHP:
namespace MyNamespace; trait MyTrait { public function exampleTrait() { echo __TRAIT__; // Output: MyNamespace\MyTrait echo __NAMESPACE__; // Output: MyNamespace } }
Commonly used PHP predefined constants:
echo PHP_VERSION; // Output: Current PHP version echo PHP_OS; // Output: Operating System echo PHP_INT_MAX; // Output: Maximum integer value supported
Defining custom constants in PHP:
define("MY_CONSTANT", "Hello, World!"); echo MY_CONSTANT; // Output: Hello, World!
Constants for error reporting and debugging in PHP:
error_reporting(E_ALL); // Report all errors ini_set('display_errors', 1); // Display errors echo E_NOTICE; // Output: 8 (constant for notice error type) echo E_WARNING; // Output: 2 (constant for warning error type)