KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
🐘

PHP

Topic Hub & Articles

PHP Intro

10 min

Php Mysql Database

10 min

PHP Install

10 min

PHP Syntax

10 min

Recap Quiz

5 Questions

PHP Comments

10 min

PHP Variables

10 min

PHP Echo / Print

10 min

Recap Quiz

5 Questions

PHP Data Types

10 min

PHP Strings

10 min

PHP Numbers

10 min

Recap Quiz

5 Questions

PHP Math

10 min

PHP Constants

10 min

PHP Operators

10 min

Recap Quiz

5 Questions

PHP If...Else...Elseif

10 min

PHP Switch

10 min

PHP Loops

10 min

Recap Quiz

5 Questions

PHP Functions

10 min

PHP Arrays

10 min

PHP Superglobals

10 min

Recap Quiz

5 Questions

PHP RegEx

10 min

PHP Form Handling

10 min

PHP Form Validation

10 min

PHP Form Required

10 min

Recap Quiz

5 Questions

PHP Form URL/E-mail

10 min

PHP Date and Time

10 min

PHP Include

10 min

PHP File Handling

10 min

Recap Quiz

5 Questions

PHP File Open/Read

10 min

PHP File Create/Write

10 min

PHP File Upload

10 min

Recap Quiz

5 Questions

PHP Cookies

10 min

PHP Sessions

10 min

PHP Filters

10 min

Recap Quiz

5 Questions

PHP Filters Advanced

10 min

PHP JSON

10 min

PHP Exceptions

10 min

Recap Quiz

5 Questions

PHP What is OOP

10 min

PHP Classes/Objects

10 min

PHP Constructor

10 min

Recap Quiz

5 Questions

PHP Destructor

10 min

PHP Access Modifiers

10 min

PHP Inheritance

10 min

Recap Quiz

5 Questions

PHP Constants

10 min

PHP Abstract Classes

10 min

PHP Interfaces

10 min

Recap Quiz

5 Questions

PHP Traits

10 min

PHP Static Methods

10 min

PHP Static Properties

10 min

Recap Quiz

5 Questions

PHP Iterables

10 min

MySQL Database

10 min

Connect to MySQL

10 min

Create Database

10 min

Recap Quiz

5 Questions

Create Table

10 min

Insert Data

10 min

Get Last ID

10 min

Recap Quiz

5 Questions

Insert Multiple

10 min

Prepared Statements

10 min

Select Data

10 min

Recap Quiz

5 Questions

Delete Data

10 min

Update Data

10 min

Limit Data

10 min

Recap Quiz

5 Questions

Progress
0%

0 / 61 Lessons

PHPPHP Tutorial
Lesson

PHP Constants

10 min reading
Free Course

PHP Constants & Magic Constants

Constants are identifiers for simple values that cannot be altered or undefined during script execution. Unlike variables, constants do not start with a dollar sign $, are globally accessible throughout script execution, and are defined using either const or define().

const vs define()

Feature const define()
Execution Timing Defined at compile-time Defined at run-time
Placement Context Allowed in global scope, class definitions, & interfaces Allowed anywhere, including inside conditionals
Case Insensitivity Never allowed Allowed in legacy PHP (Deprecated in 7.3+)
Expression Support Scalar expressions supported Full dynamic evaluation supported

Magic Constants in PHP

PHP provides 8 magic constants that change dynamically based on where they are used:

  • __LINE__: Current line number in file.
  • __FILE__: Full path and filename of current file.
  • __DIR__: Directory path of current file.
  • __FUNCTION__: Current function name.
  • __CLASS__: Current class name including namespace.
  • __METHOD__: Current class method name.
  • __NAMESPACE__: Current namespace name.
flowchart TD
    A["Constants Definition"] --> B["Global / Scope Constants"]
    A --> C["Magic Constants"]
    B --> B1["const API_KEY = 'xyz'"]
    B --> B2["define('APP_ENV', 'production')"]
    C --> C1["__DIR__ (File Directory Path)"]
    C --> C2["__FILE__ (Absolute File Path)"]
    C --> C3["__CLASS__ / __METHOD__"]

Practical Code Example

<?php
declare(strict_types=1);

namespace App\Config;

// Compile-time Constant
const MAX_UPLOAD_LIMIT_MB = 25;

// Runtime Constant Definition
define('APP_START_TIMESTAMP', microtime(true));

class Environment
{
    public const STAGING_URL = "https://staging.kodersolution.com";

    public function debugInfo(): array
    {
        return [
            'file' => __FILE__,
            'directory' => __DIR__,
            'class' => __CLASS__,
            'method' => __METHOD__,
            'line' => __LINE__,
        ];
    }
}

$env = new Environment();
echo "Max Upload Limit: " . MAX_UPLOAD_LIMIT_MB . " MB
";
echo "Staging URL: " . Environment::STAGING_URL . "
";
echo "Current File Directory: " . __DIR__ . "
";

Best Practices

  • Prefer const over define() for Hardcoded Configuration: const is cleaner, faster, and works natively inside classes and interfaces.
  • Use Magic Constant __DIR__ for Autoloading & Includes: Use require_once __DIR__ . '/../vendor/autoload.php'; instead of fragile relative file paths.
  • Use UPPERCASE_SNAKE_CASE Naming Convention: Keep constant names in uppercase letters separated by underscores (DEFAULT_TIMEZONE).

Self-Check Challenge

Write a PHP class DatabaseConfig with a public const DB_PORT = 3306; and a method getConfigFile() that returns __DIR__ . '/config.json'!

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum