PHP Defining constants

PHP

To create a constant in PHP, we have to use the define() method.Syntax:
bool define (identifier, value, case-insensitivity ) 
Parameters : the function has two required parameters and one optional parameter.
  • identifier: specifies the name that will be assigned to the constant.
  • value: specifies the value to be assigned to the constant.
  • case insensitive (optional): specifies whether the constant identifier should be case insensitive. The default is set to false, i.e. case sensitive.
Return type : This method returns TRUE on success and FALSE on failure.Here are some examples, illustrating the work of the define() function:
  • The program below illustrates the definition of case-insensitive constants: // case insensitive clients define ( "Constant" , "Hello Geeks!" , TRUE); echo constant; echo Constant; ?> Output:
    Hello Geeks! // Case Insensitive thus value is echoed Hello Geeks! 
  • The program below illustrates the definition of case sensitive constants: // case sensitive define ( "Constant" , "Hello Geeks!" ); echo constant; echo Constant; ?> Output:
    constant // Case Sensitive thus value not echoed Hello Geeks! 
    The PHP compiler will also issue a warning for the above program along with output like: "PHP Notice: Using undefined constant-constant-assumed" constant "on line 5.
Summary :
  • Constants - these are identifiers that can be assigned values ​​(string, boolean, array, integer, floating point, or NULL) that usually do not change over time.
  • Constants are scope independent and always populate global scope.
  • The define() method is used to define constants.
  • The define() method is used to test the definition of a constant.
  • The constant() method is used to return the value of a constant and NULL if no constant is defined.




Get Solution for free from DataCamp guru