Dino Geek, try to help you

How to declare a constant in PHP?


In PHP, you can declare a constant using the `define()` function or the `const` keyword. Constants are similar to variables, except that once they are defined, their value cannot be changed or undefined. This feature makes them particularly useful for values that should remain constant throughout the execution of a script, such as configuration options or fixed numeric values.

  1. Using `define()`

The `define()` function is a built-in PHP function that allows you to define a constant. The function takes two primary arguments: the name of the constant and its value. Optionally, you can specify a third argument to make the constant case-insensitive, though this is generally discouraged as it’s not a common best practice.

  1. Syntax:
    ```
    define(“CONSTANT_NAME”, “value”, [case_insensitive]);
    ```

  1. Example:
    ```
    define(“SITE_NAME”, “Example Site”);
    define(“MAX_LOGIN_ATTEMPTS”, 5);

echo SITE_NAME; // Outputs: Example Site
echo MAX_LOGIN_ATTEMPTS; // Outputs: 5
```

You can also define an array as a constant starting from PHP 7.0:
```
define(“SETTINGS”, [ “host” => “localhost”, “port” => 3306
]);

echo SETTINGS[“host”]; // Outputs: localhost
```

  1. Using `const` Keyword

The `const` keyword provides another way to define constants. Unlike `define()`, `const` is defined at compile time, meaning it’s slightly faster. Additionally, `const` can be used to define constants within classes and namespaces.

  1. Syntax:
    ```
    const CONSTANT_NAME = value;
    ```

  1. Example:
    ```
    const PI = 3.14;
    const GREETING = “Hello, World!”;

echo PI; // Outputs: 3.14
echo GREETING; // Outputs: Hello, World!
```

Using constants within a class:
```
class MyClass { const VERSION = ’1.0.0’;

public function getVersion() { return self::VERSION; } }

echo MyClass::VERSION; // Outputs: 1.0.0
$instance = new MyClass();
echo $instance->getVersion(); // Outputs: 1.0.0
```

  1. Examples of Usage

1. Configuration Constants: \`\`\`php define(“DB\_HOST”, “localhost”); define(“DB\_USER”, “root”); define(“DB\_PASS”, “password”);

$conn = new mysqli(DB_HOST, DB_USER, DB\_PASS); \`\`\`

1. Mathematical Constants: \`\`\`php const E = 2.718; const G = 9.81;

echo “Euler’s Number: “ . E; // Outputs: Euler’s Number: 2.718 echo “Gravity: “ . G . “ m/s^2”; // Outputs: Gravity: 9.81 m/s^2 \`\`\`

1. Application Paths: \`\`\`php define(“BASE\_PATH”, “/var/www/html/myapp/”); define(“UPLOADS_PATH”, BASE_PATH . “uploads/”);

echo UPLOADS\_PATH; // Outputs: /var/www/html/myapp/uploads/ \`\`\`

  1. Reliable Sources

1. PHP Manual – Constants
- [Constants](https://www.php.net/manual/en/language.constants.php)
- [define()](https://www.php.net/manual/en/function.define.php)
- [const](https://www.php.net/manual/en/language.oop5.constants.php)

1. W3Schools – PHP Constants
- [PHP Constants](https://www.w3schools.com/php/php_constants.asp)

In summary, using `define()` and `const` to declare constants in PHP provides a robust way to set immutable values, aiding in maintaining consistency and reducing errors throughout your code. By leveraging these methods, you can ensure that important values remain unaltered, improving the reliability and predictability of your applications.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use