Σταθερές PHP
- Previous Page Συνάρτησεις Σειράς στο PHP
- Next Page Επεξεργαστές PHP
Constants are similar to variables, but once defined, they cannot be changed or undefined.
Σταθερές PHP
Constants are identifiers (names) for single values. The value cannot be changed in the script.
Valid constant names start with a letter or underscore (no $ symbol before the constant name).
Note:Unlike variables, constants are automatically global throughout the script.
Set PHP Constants
To set a constant, use define()
Function - it uses three parameters:
- The first parameter defines the name of the constant
- The second parameter defines the value of the constant
- An optional third parameter specifies whether the constant name is case-insensitive. The default is false.
The following example creates aCase-sensitive Constantswith the value "Welcome to codew3c.com!":
Examples
<?php define("GREETING", "Welcome to codew3c.com!"); echo GREETING; ?>
The following example creates aCase-insensitive Constantswith the value "Welcome to codew3c.com!":
Examples
<?php define("GREETING", "Welcome to codew3c.com!", true); echo greeting; ?>
Constants are global
Constants are automatically global and can be used throughout the entire script.
The following example uses a constant within a function, even if it is defined outside the function:
Examples
<?php define("GREETING", "Welcome to codew3c.com!"); function myTest() { echo GREETING; } myTest(); ?>
- Previous Page Συνάρτησεις Σειράς στο PHP
- Next Page Επεξεργαστές PHP