Download this Blogger Template by Clicking Here!

Ad 468 X 60

Thursday, 14 March 2013

Widgets

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

PHP define function
Constants In Php

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.

<?php

Define(“COUNTRY”, “PAKISTAN”);


Print “ I Love “.COUNTRY;

?>

Share it
Share It

SHARE THIS POST   

  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →

0 comments: