Skip to main content

What are magic constants in PHP?

Junior PHP
Quick Answer Magic constants change value depending on where they are used. __LINE__ gives current line number, __FILE__ gives full file path, __DIR__ gives directory path, __CLASS__ gives class name, __METHOD__ gives method name, __NAMESPACE__ gives current namespace. Useful for debugging, logging, and building autoloaders. Always available without any import.

Answer

Magic constants like __FILE__, __LINE__, and __DIR__ provide dynamic information depending on where they are used.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice