Programming in PHPBy Peter McNulty : 20-11-2004
4.28 (118)
Constants and Comments
Previously I mentioned the usage of variables, and that they can hold data of a certain type and that data can be manipulated and changed whenever the programmer likes. A new concept is that of constants, and by their name, it's pretty easy to guess what they are! Simply put, they are variables that can't change. They are constant.
Constants are commonly used in programs to hold non-changing data such as program configurations or maximum sizes for arrays, etc. They are usually defined at the top of a program however, can be defined anywhere you like. Constants <?php Above we have a script in which a constant called BOBS_NAME is defined and then the value of it is printed to screen. By convention, constants are always upper-case. They don't have to be, although it is good to go by convention so that other developers can read your code. In order to write the value of the constant to the screen, we use the echo() function as mentioned before. Notice that we don't surround the constant name in quotes when we are obtaining it's value, because if we do, then PHP will assume it to be a string, and not a constant. Comments It is common nowadays for not a single programmer, but a group of programmers to be working on a project. Therefore, it is necessary for each developer to be able to understand all the others' code so that he can modify it successfully. If you write confusing code, then the developer reading your script will also be confused, hence it is usually advised to comment your code. Commenting your code, basically means documenting your code. You explain the main parts of the code, and what is happening. This is especially useful if you write a nice algorithm, which is particularly confusing to understand, for both yourself and others to understand it in the future. Often beginner programmers will comment almost every line of their scripts, however, over time they will comment less and less as they understand what is going on. You must decide on what level you believe the programmers will be that may read your code, and comment it for their level of expertise. Comments <?php Here we have a script which defines a constant, a variable and then writes a combination of these to the screen / web page. This is obviously a very simple script, however it has been overly commented to demonstrate the usage of the comments. There are three types of comments in PHP. Two of them specify that the rest of the line is a comment, and the other one specifies that until the closing comment tag is found, is all a comment. The // and # characters are used for the single line comments. These are commonly used when you don't really have that much to say, except maybe to specify where in your program you are. These comments can be placed at the beginning of a line, or after the final semicolon after a statement on a line. The other comment type is the /* … */ comment. This type of commenting allows you to write as much text as you would like to document (not execute) the script. This is the most common method for multi-line comments. It is also particularly useful if you will be programming on both Linux/Unix and Windows systems, as if the line endings are lost in transferring from one system to another, these comments will not make the whole file a comment. This is particularly hard to explain, however, if it ever happens to you, you'll never use // and # comments again. One more thing to notice in this script is the usage of the echo function. Usually we just use it to write some text to the screen or write the value of a variable to the screen. However, this time it writes both a variable and a constant to a screen. In order to do this, we concatenate the strings (join them together) using a period (.). This can be done for as many variables, constants and strings as you like: Echo usage <?php Now we will move onto the more interesting aspects of programming, and what truly makes a program do something useful. Build Your Own Database Driven Website Using PHP & MySQL |
Ads
|