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 Magic Constants and Predefined Constants

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

  1. __LINE__: The current line number of the file.
  2. __FILE__: The full path and filename of the file. If used inside an include, the name of the included file is returned.
  3. __DIR__: The directory of the file. If used inside an include, the directory of the included file is returned.
  4. __FUNCTION__: The function name.
  5. __CLASS__: The class name.
  6. __METHOD__: The class method name.
  7. __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:

  1. PHP_VERSION: The current PHP version as a string.
  2. PHP_INT_MAX: The maximum integer size.
  3. PHP_EOL: The correct 'End Of Line' symbol for this platform.
  4. DIRECTORY_SEPARATOR: The correct directory separator for this platform (either '/' or '').
  5. 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)
  1. __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
    
  2. __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
        }
    }
    
  3. __NAMESPACE__ and __TRAIT__ in PHP:

    namespace MyNamespace;
    
    trait MyTrait {
        public function exampleTrait() {
            echo __TRAIT__;    // Output: MyNamespace\MyTrait
            echo __NAMESPACE__; // Output: MyNamespace
        }
    }
    
  4. 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
    
  5. Defining custom constants in PHP:

    define("MY_CONSTANT", "Hello, World!");
    echo MY_CONSTANT;  // Output: Hello, World!
    
  6. 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)