When you start programming in PHP or any other programming language, one of the first things you learn is the constants and variables. These constants and variables are basically the placeholders, where all of your data resides. Not only input information entered is stored in these placeholders but the results of all the intermediate calculations and ultimately the final result obtained after processing are also stored there. These constants and variables actually epresent memory locations in the RAM to us in an extremely user friendly manner. Otherwise everybody, while programming, would have to remember and mention all those hard to remember and complex hexadecimal addresses to process and manipulate data to come up with the meaningful information at the end.
First of all, let us look at constants. Constants are those placeholders which, once defined at the start, remain the same throughout the execution of the program and can not be changed even once, after being allotted a legal and meaningful value. So normally those values are allotted to constants, which have to be used frequently in the programming script and will stay the same like the value of PI(3.14….). Constants in PHP are declared and defined by using define() function and remember as a best practice, in order to distinguish variables from constants, constants are always depicted using capital letters. Let us look at an example,
<?php define(“PERSON”, “Yoshiro Yamashita”); echo “My best friend is ”. PERSON; ?>
So the above programming code will give this output, “my best friend is Yoshiro Yamashita”. This value of PERSON is not going to change throughout the program. Here the syntax of the define() function is as follows.
bool define(string CONSTANT, $value);
Here CONSTANT is the name of the constant, always depicted in upper case, $value is the value allotted to the constant and be of integer, float, string or Boolean nature. If the function is successfully executed then it returns TRUE otherwise it returns FALSE. Now we will see variables, like constants these were also placeholders to store data. But unlike constants, these variables can used to store the ever changing and transforming data in the programming script. These variables’ names start with a dollar sign ($) and are case sensitive. Care should be taken so that the first letter of the variable should only be an underscore or any alphabetic letter. Variables should not start with numbers or any other special character. Unlike many other programming languages, variables in PHP are not declared explicitly before they are actually used and can be used straightaway whenever needed. Let us look at this example where a variable is being used,
<?php define(“PERSON”, “Yoshiro Yamashita”); echo “My best friend is ”. PERSON; $sport = “Judo”; echo “He was word champion in sport of “. $sport; ?>
So if you want to learn programming in PHP, it is important to get yourself well acquainted with constants and variables, since these are the entities which store the data to be processed by the PHP program.