A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script. By default a constant is case-sensitiv. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.
To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically.
constant() function:
As indicated by the name, this function will return the value of the constant.
This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.
constant() example:
<?php define("MINSIZE", 50); echo MINSIZE; echo constant("MINSIZE"); // same thing as the previous line ?> |
Only scalar data (boolean, integer, float and string) can be contained in constants.
Differences between constants and variables are:
- There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
- Constants cannot be defined by simple assignment, they may only be defined using the define() function.
- Constants may be defined and accessed anywhere without regard to variable scoping rules.
- Once the Constants have been set, may not be redefined or undefined.
Valid and invalid constant names:
// Valid constant names define("ONE", "first thing"); define("TWO2", "second thing"); define("THREE_3", "third thing") // Invalid constant names define("2TWO", "second thing"); define("__THREE__", "third value"); |
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. For example, the value of __LINE__ depends on the line that it's used on in your script. These special constants are case-insensitive and are as follows:
A few "magical" PHP constants ate given below:
Name | Description |
---|---|
__LINE__ | The current line number of the file. |
__FILE__ | The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances. |
__FUNCTION__ | The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. |
__CLASS__ | The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. |
__METHOD__ | The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive). |
9 comments:
Thank you sharing this kind of noteworthy information. Nice Post.
Article submission sites
Technology
Your blog has very useful information about this technology which i am searching now, i am eagerly waiting to see your next post as soon
PHP Training in Chennai
PHP Course in Chennai
PHP Training in Velachery
PHP Training in Tambaram
PHP Training in Anna Nagar
PHP Training in OMR
Ethical hacking course in chennai
Hacking course in chennai
Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
Hacking Course in Coimbatore
Hacking Classes in Coimbatore
ielts coaching center in coimbatore
Java course in coimbatore
Best Oracle Training institute in Coimbatore
PHP course in coimbatore
Selenium course in coimbatore
seo course in coimbatore
SASVBA is recognized as the best machine learning training in Delhi. Whether you are a project manager, college student, or IT student, Professionals are the best machine learning institute in Delhi, providing the best learning environment, experienced machine learning instructors, and flexible training programs for the entire module.
Machine Learning Training in Delhi
SASVBA Provides Best Business Analysts/ Business Intelligence / Power BI Training in Delhi/NCR with Latest Development Environment and Frameworks. We keep Our Courses Up to Date with the Latest industrial trends.
Power BI Training in Delhi
SASVBA offers one of the deepest SAS Training in Delhi. SAS certified students can quickly apply for jobs at MNC. Participants must enroll in the best SAS training institute in Delhi to acquire technology-related skills. Our main strength is our trainers, who are seasoned professionals and experienced in their industry.
SAS Course in Delhi
SASVBA Delhi offers comprehensive Excel and Advanced Excel Training in Delhi. Extensive hands-on labs delivered by Excel and the Advanced Excel Institute in Delhi include live projects and simulations. Such detailed Excel and Advanced Excel courses have helped our students to get jobs in various multinational companies
Advanced Excel course in Delhi
The course covers generation of mobile phones, components used in mobile phones, troubleshooting mobile phone hardware (water damage, freezing, charging problems, network problems, power failure, keyboard problems, etc.), installation software Includes flashing, use of secret codes.
Mobile Repair Service Near Me
Good article.Don’t you get irritated by restrictions for some sites you want to access? you can check one article about Access Blocked Content you will, stop getting irritated,bypass the restriction, and access the site you want using Node unblocker.
Post a Comment