How can errors related to undefined constants be avoided in PHP?

When working with constants in PHP, it's important to define them before using them to avoid errors related to undefined constants. To avoid these errors, make sure to define constants using the define() function before referencing them in your code.

// Define the constant before using it
define('MY_CONSTANT', 'Hello World');

// Now you can safely use the constant in your code
echo MY_CONSTANT;