PHP Programing language

Constants in PHP
Previous Home Next
adplus-dvertising

Constants

A constant is an identifier for a simple value. A constant value cannot be modified during the execution of the script. By default a constant is case-sensitive. It can be defined by using define() function or by using keyword const outside a class definition.

<?php
define("Student_name""Ankur");
echo Student_name;
?>

Output:

Ankur

Valid and Invalid constant names:

<?php  
//Valid constants name  
define("student_name""Ankur");  
define("class""V");

//Invalid constants name  
define("2NO_OF_STUDENTS", 60);  
define("__Total__", "total value"); 
?>

PHP Magic constants:

PHP provides a large number of predefined constants to any script which it runs.

SNo. Name Description
1 __LINE__ The current line number of the file.
2 __FILE__ The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
3 __CLASS__ The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
4 __TRAIT__ The trait name. The trait name includes the namespace it was declared in (e.g. Foo\Bar).
5 __METHOD__ The class method name.
6 __FUNCTION__ The function name.
7 __NAMESPACE__ The name of the current namespace.
8 __DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
Previous Home Next