Thursday, 14 March 2013
Complete Php Tutorial On Constants And define() Function
Why To Use Constants In Php ?
We have discussed and learnt in the previous tutorials that we can store data in variables temporarily and data in variables can be changed. But sometimes we require such a value that we want to be changed never. For this purpose we use Constants.
What Is A Constant ?
Constant is type of variable to which we assign a value only once and then the assigned value never changes.
Difference Between Variable And Constant And Their Definition :
There is a Difference Between Variable And a Constant .For defining a variable , we just add $ sign to the name of variable. For defining a constant in Php, there is a function in php which we have to use. The name of the function is Define. This function gets two parameters input. First we write the name of the constant and second we write the value which we want to assign to the constant.
For Example :
define (“CONSTANT_NAME”, “MyTricksLab”);
Here in the above given example we have given define a constant with the name CONSTANT_NAME and then we have assigned it the value MyTricksLab. Here it is important to note that we can only assign a number or string value to a constant.
If we want to get the value of any constant, then we only write the name of the constant and we do not use $ sign with the name of the constant. For understanding this, Just see the script below.
<?phpDefine(“COUNTRY”, “PAKISTAN”);Print “ I Love “.COUNTRY;?>
Share It |
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →
Related Posts:
PHP Programming Tricks and Tutorials Web Development
Subscribe to:
Post Comments (Atom)
0 comments: