Computer Magic
Software Design Just For You
 
 



PHP Tutorial – Lesson 5 Variables are those little furry creatures right?

Now it is time to do some real programming… sort of. We want to start talking about the concept of a variable. Variables are simply places to hold information while the programming is running. Notice that I said “while the program is running”. Computers have memory, and they have hard drives. These are the primary storage mediums in a computer. Hard drives are cool, they store stuff permanently. Anything that is on the hard drive is still there if the power goes out. Memory is cool, it stores stuff in memory until the power goes out (power spike, turning off/rebooting the computer, etc).

Why use memory at all then? If the hard drive is permanent, what good is memory? Given our current discussion, it isn’t much good. Lets look at access speed though. A hard drive is measured in mili seconds. Most modern hard drives fall between 6-14 ms seek time. This the time it takes to locate a piece of data on the drive and start to read it. The problem is, hard drives are so huge, that the only way you can locate data on the drive (without scanning the whole thing) is to have an index that points to data locations. This index is your FAT table (FAT 32, NTFS, ext2, etc). This is great, but the problem is that you need to do a read of the index table to find the data before you can read it. Now, you have more than one seek possible. Once you have located a piece of data on your hard drive and the head is in position, the process of actually reading the data begins. If this data is large, it might take a while to actually transfer the data from the drive to the CPU (limited by bandwidth). Have you seen the UDMA 33, 66, 100, 133 ratings on drives? This corresponds to the bandwidth of the drive.

Now, lets talk about memory. The involved process of working with a drive isn’t the bad part, it is the seek time and bandwidth limits that causes the bottleneck. With memory, your seek time is measured in nano seconds and typically falls in the 6-14 range also. Additionally, memory doesn’t use an indexing algorithm to locate data, you simply keep track of the address and jump directly to the address. Since Memory has no moving parts, and a seperate index lookup isn’t required, memory is significantly faster. Memory bandwidth is also MUCH larger (bump that UDMA rating from 133 to 2000 or more!). The speed of memory is the reason that we have both a hard drive (for permanent storage) and memory (for temporary storage). Memory is our work space, hard drives are our file cabinents.

I mentioned in the previous paragraph that the memory is addressed directly rather than looked up. This means no nice file paths like a hard drive to keep track of our memory (c:\my documents\my picture.jpg). Do you want to remember AOFD243AA? That would be just madening (try doing some assembly language programming!). One way that programmers have come up with to ease the burden of keeping track of memory address is to set names. These named memory addresses are called variables. In PHP (and in all modern programming languages) you can simply use your variable without worrying where it is actually located.

Ok, enough theory… Lets do some code!!!



<?php
$first_name = "bob";
print "Your name is: $first_name";
?>

In PHP, you don’t actually have to create a variable. In some languages, you have to create a variable before using it. Here, to create a variable, you just assign it a value (“bob”). Notice that bob is in “s. Bob is data, not code, and therfore needs to be in “s. Also, notice that both lines (the assignment and the print) end with the ; symbol. Again, this is important to tell PHP that the current line is done (you can actually place them on the same line as long as the ; seperates them and it will work!).

To assign a value in PHP (and in most languages) you simply use the = sign. One thing that throws people new to programming is that the equation is written backwards. The value moves to the left. In kindergarden, we learned that 10+10=20. Here, we are learning that 20=10+10. It all still works, no change in the math, just a different way to write it.

All variables in PHP start with the $ symbol. This is a clue to PHP that the following word is a variable ($first_name). In the print line, notice that the variable is actually inside the quotes. This will still work (prints “Your name is: bob”) because the $ symbol tips PHP off that there is a varaible inside the string and it can replace the $first_name with the actual first name. A few other languages do this also (Perl for one).

Variables in PHP can be strings (a group of characters) or numbers. PHP handles the difference internally and has some great auto conversion features. All you have to do is assign the value and PHP handles it.



<?php
$var = 3;
$var = "Hi";
$var = "10";
$var = "the quick brown fox";
$var = 25;
print "The variable is $var";
?>

This will assign various values to the variable, and then print out the current value. Notice that only the last value is printed, as you assign a new value, the previous one is lost!

Once common usage for variables is to get the value send to the script. Here is an example:



<?php
$first_name = $_GET["first_name"];
$last_name = $_GET["last_name"];
print "Your name is $first_name $last_name";
?>

Add the ?first_name=bob&last_name=smith to the URL for this page, and watch it echo your name back to you. By grabbing the value out of the $_GET array to start with, it makes the code cleaner and easier to read later. It also gives you a chance to check the values coming in before you use them (what if they didn’t fill out the name at all, or if they injected some hacking code into it?).

There is LOTS more information on varaibles, look at other tutorials on the internet. I will be writing more on them soon (ever hear of variable varaibles?).

Ray Pulsipher

Owner

Computer Magic And Software Design

Comments are closed.


Home | My Blog | Products | Edumed | About Us | Portfolio | Services | Location | Contact Us | Embedded Python | College Courses | Quick Scan | Web Spy | EZ Auction | Web Hosting
This page has been viewed 616097 times.

Copyright © 2005 Computer Magic And Software Design
(360) 417-6844
computermagic@hotmail.com
computer magic