constants in PHP
Constants
are variables whose values cannot be changed during the script
execution.
Two ways to define a constant:
- Using the define () method.
- Using the const keyword.
- constant () function
Note: - naming a
constant, don't use $ symbol with the constant's name.
1.
Using define ()
To create a constant.
Syntax: -
Define (name, value,
case-insensitive)
Parameters:
Name: Name of the constant
Value: Value of the constant
Case-insensitive: specifies whether the constant name is case sensitive or not. Its default value is false, which means, by default, the constant name is case-sensitive.
1. <?Php 2. 3. // this creates a
case-sensitive constant 4. Define("name",
"ram"); 5. Echo name, "\n"; 6. 7. // this creates a
case-insensitive constant 8. Define("name1",
"ram", true); 9. Echo name1; 10. 11. ?> |
Output:
Ram
Ram
2.
Using the const
keyword
Define constants in PHP using the const
keyword.
Const
keyword
used to define scalar constants, i.e. Only integers, floats, booleans, and
strings, not for an array.
Define
()
can be used for an array.
Syntax
Constant(name)
<?Php Const mesg="hello i am php
constant "; Echo mesg; ?> |
Output:
Hello i am php
constant
Constants
are global:
by default, constants are automatically global, and can be
used throughout the script, accessible inside and outside of any function.
Example:
<?Php Define ("abc",
"hello constants"); Function xyz() { echo
abc; } Xyz(); ?> |
Hello constants
3.
Constant () function
For constants using a constant () function
instead of the echo statement.
Syntax
The syntax for the following constant
function:
1.
Constant (name)
<?Php
define("msg", "sjkpgm");
echo msg, "</br>";
echo constant("msg");
//both are similar
?>
Output:
Sjkpgm
Sjkpgm
=========================================================
Php magic constants
- Php provides a large number of predefined constants to any script which it runs.
- There are five magical constants that change depending on where they are used.
- These special constants are case-insensitive and are as follows −
Sr.no |
Name & description |
Syntax |
Example |
1 |
__line__ |
The current line number of the file. |
1. <?Php 2. Echo __line__; // result = 2 3. Echo __line__; // result = 3 |
2 |
__file__ |
The full path and filename of the file. |
Echo __file__; // result = "/full/path/of/this/file.php" |
3 |
__function__ |
Returns the function name as it was
declared (case-sensitive). |
Function hellofunction() { echo __function__; } hellofunction(); // result = "hellofunction" |
4 |
__class__ |
Returns the class name as it was declared (case-sensitive). |
Class helloclass { public function hellomethod() { echo __class__; } } $x = new helloclass; $x->hellomethod(); // result = "helloclass" |
5 |
__method__ |
Returned as it was declared
(case-sensitive). |
Class helloclass { public function hellomethod() { echo __method__; } } $x = new helloclass; $x->hellomethod(); // result =
"helloclass::hellomethod" |
6 |
__namespace__
|
Returns the name of the current namespace in use. |
Namespace hellonamespace; echo __namespace__; //result =
"hellonamespace" |
7 |
Classname::class
|
Returns the fully qualified class name, it is really handy when
using namespaces. |
Namespace hellonamespace; class helloclass { } echo helloclass::class; // result = hellonamespace\helloclass |
8 |
__trait__
|
Return the name of the trait that we are currently using. It will
also prepend the trait with the current namespace as well. |
Namespace hellonamespace; trait hellotrait { function hellomethod() { echo __trait__; } } class helloclass { use hellotrait; } $x = new helloclass; $x->hellomethod(); // result =
"hellonamespace\hellotrait" |
9 |
__dir__
|
Return the full directory path of the currently executed file. |
Echo __dir__; // result = "/full/directory/path/of/file" |
==============================
0 Comments