Global PHP Constants | PHP Gurukul

 


What is PHP Constants?

Constants are variables
Constants are variables except that once they are defined they can’t be changed or undefined.

  • A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
  • A valid constant name starts with a letter or underscore.
  • (no $ sign before the constant name).
  • Note: Unlike variables, constants are automatically global across the entire script.
How to create a PHP constant
  • You can create a constant by using the define () function.
  • Use the define function to specify the name and value of a constant. (name, value, case-insensitive).
  • Many programmers use capital letters to name constants – so they stand out.
  • Note: Since a constant can not be change you do not use a $ sign before it’s name when you declare it or (use it).

Parameters
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

How to declare constants

Examples:

PHP Constant Examples

Output: Welcome to PHPGURUKUL.com!

Click : https://phpgurukul.com/php-constant/

Difference Between Constant and Variables
  • The mentioning of a dollar sign($) is the first evident difference between constants and variables. Variables have to be preceded by a dollar sign while constants are specified without it.
  • Variables are assigned values using the simple assignment. However, constants cannot be defined in this manner. Constants can only be defined using define() function.
  • Variables can be assigned, reassigned and un-defined. How-ever, constant cannot be modified in any way during the course of program.
  • The Variable scoping rules do not apply to contants. They can be accessed and defined anywhere
Magic Constants

There are predefined constants supported by PHP. Any script that runs on PHP can access these constants. Some constants in PHP change on the basis of where they are being used. For instance, the constant ___LINE___ is dependent on the line that is being used. These are case-insensitive. Some of the PHP constants, where are defined as ‘magical’ are as follows-

Magic ConstantMeaning
___LINE___This constant indicates the present line number.
___FILE___This constant contains the path and file name of the file concerned. It is important to mention here that the path contained by this constant is the absolute path.
___FUNCTION___This constant includes the name of the function as it was when the function was declared. The name is case-sensitive
___CLASS___This constant returns the name of the class concerned in a case-sensitive manner and exactly in the manner that it was declared.
___METHOD___This constant returns the name of the method concerned in a case-sensitive manner and exactly in the manner that it was declared.
Blog : https://phpgurukul.com/php-data-types/

 

PHP Gurukul

Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.

Website : https://phpgurukul.com

Comments

Popular posts from this blog

Pre-School Enrollment System using PHP and MySQL | PHP Gurukul

Global Food Ordering System Using PHP and MySQL | PHP Gurukul

Doctor Appointment Management System Using PHP and MySQL | PHP Gurukul