Coder’s Corner / PHP Tutorial 001: Intro to Numbers and Strings
If you want to learn how to write PHP scripts you have to start someplace. Understanding how your scripts will store data for presentation and manipulation is vital, so right off the bat we’ll take a look at numbers and strings.If you want to learn how to write PHP scripts you have to start someplace. Understanding how your scripts will store data for presentation and manipulation is vital, so right off the bat we’ll take a look at numbers and strings. This is the first tutorial in what will be an ongoing series on basic PHP coding. Look for new tutorials to appear several times each month; follow along and soon you will be spicing up your Web sites with custom PHP scripts that will separate your work from the pack.
A few months ago I wrote an article titled PHP and Dynamic Adult Web Sites for TheAdultWebmaster.com’s Coder’s Corner. The purpose of that article was to introduce adult Webmasters to the benefits of the PHP programming language. (If you have not yet read that article then you might want to do so either before or immediately after reading this article.) I have found PHP easy to learn, and even if you don’t plan on writing your very own scripts it’s not a bad idea to have a workable knowledge of PHP. At the very least, understanding PHP basics will allow you to modify and customize third party PHP scripts to better suit your specific purposes. With that in mind, this article will be the first in an ongoing series on PHP programming for beginners. I will do my best to present each tutorial in a manner suitable for Webmasters with little or no prior coding experience; simply check TheAdultWebmaster.com often for new tutorials.
STORING DATA AS NUMBERS AND STRINGS
Almost any PHP script worth writing will need to store and manipulate data of some kind. If you want to write a simple script that tracks raw clicks to your Web site, for example, then you will need to store your running “total clicks” count somehow. That’s one example of data. If you want to write a more advanced counter then you might need to store other forms of data, such as the total number of unique clicks, referring Web site URLs, or the browser used by your visitors. So whenever you sit down to write a new PHP script, you will want to first spend a significant amount of time deciding exactly what data your program will store and manipulate.
PHP can store data in two different ways: as a number or as a string. Furthermore, numbers can be stored either as an integer or as a double. Don’t get nervous, all of this will be explained in nauseating detail below. So what’s the difference between a string and a number? Well the quick and short answer is this: a PHP script uses numbers to store data that it plans to use in mathematical computations, while a string is used to store data in a manner that is readily usable by us carbon-based life forms. Again, this will be explained in detail below, so nobody panic!
NUMBERS: INTEGERS AND DOUBLES
MORE ON INTEGERS
At this stage it’s important that you can tell the difference between valid and invalid integers. Remember that a valid integer should not contain any decimal points, punctuation marks, spaces or letters, and it must fall in the acceptable value range mentioned above. With all that in mind, here are some examples:
2673627(Valid integer.)
1.201(Invalid due to the decimal point.)
-1202(Valid integer.)
0(Valid integer.)
1,281(Invalid due to the comma.)
2147483648(Invalid because this number is too large.)
MORE ON DOUBLES
Like integers, a valid double should not include any extra spaces or commas, but must include a decimal point. Most letters are also inappropriate for doubles with one exception which will be explained shortly. Here are some examples of valid and invalid doubles:
1.231(Valid double.)
-33.01(Valid double.)
3,231.01(Invalid due to the comma.)
0.0(Valid double.)
21(Invalid due to lack of decimal point.)
2.5e3(Valid double, see below for explanation.)
WHAT ABOUT STRINGS?
Okay, enough of the math class for a little while, let’s talk about strings instead. Strings are used to store data that is either collected from or presented to human beings. Examples? Someone’s name would be stored as a string, as would their Web site URL and their telephone number. Wait a second, you say? A telephone number is numerical data, you say? Well it does consist of numbers, in the vast majority of the time you will not be performing any mathematical computations on a telephone number, so we generally store those as strings and not numbers. In fact, strings can be used to store any kind of numerical data. If it turns out that the data in question is numerical and needs to be mathematically manipulated, we simply convert the data from a string to a number, perform the mathematical manipulations, then convert it back to a string. It sounds like a pain in the ass, and sometimes it is, but converting data is more effective than simply performing mathematical calculations on strings. Your computer will thank you for the effort. The specifics for how to convert data from a string to a number and back again will be covered in a later tutorial.
When it comes time to specify the data for a string in your PHP script, you can do by enclosing the desired data between double quotes. For example, “Connor Young”. Numerical data being stored as a string would also be specified with the double quotes method, such as “12.00” or “21”. Don’t worry, all this will make sense later as you read future tutorials – right now all you need to know is that data stored as a string will be surrounded by double quotes in your PHP scripts.
Sometimes it’s necessary to use an “escape sequence” in your string to specify certain text characters that would confuse PHP if they were merely typed on the screen as they would normally appear. Consider the following escape sequences, which all consist of a backslash followed by a text character:
\”double quote
\$dollar sign
\nlinefeed
\rcarriage return
\thorizontal tab
\\backslash
For an example of how escape sequences work in strings, consider the following line of text:
She said: “Get lost!”
Simply assigning the above text to a string by enclosing it in double quotes would confuse PHP because the above line includes double quotes of it’s own. PHP would arrive at the first double quote in that text and mistakenly think you were trying to finish the string, then it wouldn’t know what the heck to do with the remaining text and would terminate the program with a parse error. The proper way to specify the above line as a string would be to use the proper escape sequence for the double quotes. Here is how that would look:
“She said: \”Get lost!\””
If you wanted to include a carriage return followed by a linefeed after the above line of text then you would type the following:
“She said: \”Get lost!\”\r\n”
A QUICK GLANCE BACK
Understand that at this point you should have absolutely no idea how to write even the most basic PHP scripts, so if you feel confused then you’re on the right track! What you should understand at this point is that PHP scripts store data as either a number or a string. You should also understand that numbers can be stored as either an integer or a double. Spend a little more time making sure you understand the difference between valid and invalid integers and doubles. At this point you should also have a basic understanding of strings and escape sequences. As you progress with future tutorials your understanding of and comfort with these principles will increase.
If you have any questions about the information in this tutorial or simply wish to make a comment then click here for our Tech Talk forum.
Connor Young is Editor-in-Chief of The ADULTWEBMASTER Magazine.