Chapter 0:Introduction 0-000.000 - About This Course
Chapter 1:PHP Basics 1-1.1 - Hit the ground running.
1-1.2 - A Simple PHP Program
1-1.3 - Formatting the code
1-1.4 - Comments in PHP
1-1.5 - Storing Data in Variables
1-1.6 - The print() Function
|
Chapter: 1 - PHP Basics
Lesson: 1.5 - Storing Data in Variables
As you have probably already noticed, in Listing 1.2, 1.3 and 1.4 the program we have been working with calls the built in PHP Function "date" to determine todays date and format it to a user readable form. What we did with that data was to store it in a variable called "$date" so that we could use it when we were ready to send the output to the user's browser.
Without the ability to store data in variables, we would have to use the data as soon as it was created. Think of it this way, a variable is like your refrigerator, when you buy milk you don't use it all right then. Your milk (data) needs to be kept someplace until you are ready to use it or it will go bad. So we store the data in an appropriately named variable so that we can easily ask for the data again later when we need it.
When we called the print() function (another built in function with PHP) we asked for PHP to give us the information that we had previously stored in the variable "$date", which was the formatted representation of today's date. So, you have some idea of how variables work. Let's put them to work for us.
As you see below in Figure 1.5, this program extends our example from previous lessons to personalize the output a little more by displaying my name and the number of days remaining until my birthday. I realize that this is not very useful right now, but... We are still building our base knowledge of the basics so that we can write programs with more useful features later in this tutorial.
Figure 1.5, Date program expanded. (download code here)
(Try the sample code)
As a keen observer, you probably noticed that my last print() statement spanned two lines this time. We did this by closing the quotation, appending a "." after a space and carrying over to the next line beginning with another quotation mark and closing as usual. This is helpful when long statements need to carry over multiple lines or, as in this case, I need to keep the code short for imaging it.
You should have also noticed that I was able to store my name and number of days until my birthday in the variables named accordingly. To declare these variables, I simply used them. PHP is very pleasant in this regard. I didn't have to declare them before populating them, I simply said that $name = "Steve" to populate $name with the text "Steve". PHP is also kind enough to declare the data type for us as well.
Then, when I needed that data in my print() statement, I just called it back up using the same variable name and poof, there it is. I would suggest that you re-write this code using your own name and birthday count so that you become comfortable with storing data in variables and using that data in your programs.
There will be more lessons on variables later on, but for now, you have the basics.
|