"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Saturday, January 20, 2024

PHP Constants

 PHP Constants

PHP constants are name or identifier that can't be changed during the execution of the script except for magic constants, which are not really constants. 

PHP constants can be defined by 2 ways:

  1. Using define() function
  2. Using const keyword

Constants are similar to the variable except once they defined, they can never be undefined or changed. They remain constant across the entire program. 

PHP constants follow the same PHP variable rules. For example, it can be started with a letter or underscore only.

Conventionally, PHP constants should be defined in uppercase letters.     

constants are automatically global throughout the script.

PHP constant: define()

Use the define() function to create a constant. It defines constant at run time. Let's see the syntax of define() function in PHP.

Syntax: define(name, value, case-insensitive)  

  1. name: It specifies the constant name.
  2. value: It specifies the constant value.
  3. case-insensitive: Specifies whether a constant is case-insensitive. Default value is false. It means it is case sensitive by default.

File: constant1.php

<?php  

define("MESSAGE","Hello  PHP");  

echo MESSAGE;  

?>  

Output:  Hello PHP

Create a constant with case-insensitive name:

File: constant2.php

<?php            

define("MESSAGE","Hello PHP",true);//not case sensitive    

echo MESSAGE, "</br>";    

echo message;    

?>    

Output:

Hello PHP

Hello PHP


File: constant3.php

<?php  

define("MESSAGE","Hello PHP",false);//case sensitive  

echo MESSAGE;  

echo message;  

?>  

Output:

Hello PHP
Notice: Use of undefined constant message - assumed 'message' 
in C:\wamp\www\vconstant3.php on line 4
message

PHP constant: const keyword

PHP introduced a keyword const to create a constant. The const keyword defines constants at compile time. It is a language construct, not a function. The constant defined using const keyword are case-sensitive.

File: constant4.php

<?php             

const MESSAGE="Hello const by JavaTpoint PHP";  

echo MESSAGE;  

?>  

Output: 

Hello const by JavaTpoint PHP

Constant() function

There is another way to print the value of constants using constant() function instead of using the echo statement.

Syntax                        constant (name)  

File: constant5.php

<?php      

    define("MSG", "PHP");  

    echo MSG, "</br>";  

    echo constant("MSG");  

    //both are similar  

?>  

Output:

PHP

PHP 

 Constant vs Variables

Constant

Variables

Once the constant is defined, it can never be redefined.

A variable can be undefined as well as redefined easily.

A constant can only be defined using define() function. It cannot be defined by any simple assignment.

A variable can be defined by simple assignment (=) operator.

There is no need to use the dollar ($) sign before constant during the assignment.

To declare a variable, always use the dollar ($) sign before the variable.

Constants do not follow any variable scoping rules, and they can be defined and accessed anywhere.

Variables can be declared anywhere in the program, but they follow variable scoping rules.

Constants are the variables whose values can't be changed throughout the program.

The value of the variable can be changed.

By default, constants are global.

Variables can be local, global, or static.

 

Magic Constants

Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore.

They are similar to other predefined constants but as they change their values with the context, they are called magic constants.

There are nine magic constants in PHP. In which eight magic constants start and end with double underscores (__).

  1. __LINE__
  2. __FILE__
  3. __DIR__
  4. __FUNCTION__
  5. __CLASS__
  6. __TRAIT__
  7. __METHOD__
  8. __NAMESPACE__
  9. ClassName::class

All of the constants are resolved at compile-time instead of run time, unlike the regular constant. Magic constants are case-insensitive.

 

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template