Php constants
Constants
are variables whose value 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. It's default value is false,
which means, by default, the constant name is case sensitive.
4. <?Php 5. 6. // this creates a
case-sensitive constant 7. Define("name",
"ram"); 8. Echo name, "\n"; 9. 10. // this creates a
case-insensitive constant 11. Define("name1",
"ram", true); 12. Echo name1; 13. 14. ?> |
Output:
Ram
Ram
2.
Using the const
keyword
Define constants in php using the const
keyword.
Const
keyword
use for to define scalar constants, i.e. Only integers, floats, booleans and
strings, not for array.
Define()
can
be used for 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 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