What is the difference between using define() and const for setting constants in PHP?

The main difference between using define() and const for setting constants in PHP is that define() is a function that can be used anywhere in the code, even inside functions or loops, while const can only be used at the class level. Additionally, define() allows for dynamic constant names, while const does not. It is generally recommended to use const for class constants and define() for global constants.

// Using define() for setting global constants
define('SITE_NAME', 'My Website');
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'password');

// Using const for setting class constants
class MyClass {
    const PI = 3.14;
    const MAX_USERS = 100;
}