PHP CHAPTER 12

PHP Constants

A constant is a named value that should not change during normal program execution.

📘

What is it?

A constant is a named value that should not change during normal program execution.

💡

Why do we use it?

Constants are useful for application names, fixed limits and configuration values.

⚙️

How does it work?

PHP creates a constant using define() or const and the value is accessed without $.

🧩

Syntax

PHP Syntax
define("NAME", "value");
💻

Example

PHP Example
<?php

define("SITE_NAME", "AIFA Infotech");
echo SITE_NAME;

?>
🔍

Example Explained

define creates SITE_NAME. The constant is accessed directly without a dollar sign.
🚀

More Examples

<?php const MAX_STUDENTS = 100; echo MAX_STUDENTS; ?>

⚠️ Important Rules

Choose clear names for constants and avoid redefining them.

❌ Common Mistakes

Trying to access a constant with $ or changing its value.

🌍 Real-World Use

Application configuration and fixed business rules.

📝 Practice Exercise

Create constants for site name, support email and maximum students.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →