PHP Constant


The define() function is used to define constants. Once a constant is defined, it can not be changed or undefined.

An example of defining constants:

<?php
 define("CONSTANT", "Hello!");
 echo CONSTANT;
?>

Constant names and any other PHP tags follow the same naming convention. Valid constant names start with a letter or an underscore followed by any letter, number, or underscore.

Constants are case-sensitive by default, and conventionally constant identifiers are always uppercase and can not be changed during script execution.

Define the difference between a constant and a defined variable:

  • Constant without a dollar sign ($)
  • Constants can only be defined with define() function, but not through the assignment statement
  • Constants can be defined and accessed anywhere without regard to the rules of the variable scope
  • Constants can not be redefined or undefined once they are defined
  • The value of a constant can only be scalar

PHP built-in a large number of predefined constants, the specific can refer to the PHP manual.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook