Let’s put together an example using some of the things we’ve seen at this point. What I’d like to do in this example is I’d like to put together a page that, given the known speed of light and a certain number of days, let’s calculate how far light would travel in that number of days. I’m going to start off here in my example file by jumping up to the top of the page and I’m going to put in a set of PHP tags where I’ll do some of the basic calculations, some of the basic gathering of data that’s going to be necessary for the rest of this script.
Let me start off by creating a lightspeed variable. As a matter of fact, instead of creating a lightspeed variable, since the speed of light never changes let’s create a lightspeed constant. I’m going to use the define function, I’m going to create a new variable called lightspeed and I’m going to set it to 186,000 miles per second. I’ll put a comment in here, miles per second. There we go. And let’s say the other piece of data that we know is a particular number of days. For that I’m going to create a days variable because that’s something that potentially in the future we may want to change. Let’s say seven days. How far will light travel in a week, basically. Once I have those two basic pieces of data I could then use those to figure out exactly what I need, the actual distance that’s traveled.
Since the speed of light I have is in miles per second and what I basically want to figure out here is miles per day, what I need to do is I need to take the number of days, seven, and I need to convert that into seconds. Let me do that by creating a seconds variable and to get the value of seconds I’m going to take whatever the current value of my days variable is and I’m going to multiply it by 24 – that’ll tell me the number of hours in that number of days. Then I’m going to multiply that by 60, which will tell me the number of minutes in that number of hours. And then I’ll multiply that by 60 again so now I have the number of seconds in the number of minutes in the number of hours in that number of days. To actually figure out the distance then, which I’ll keep in a new variable I’m going to call distance, what I’ll do is I’ll simply take my number of seconds and I’ll multiply that by my lightspeed constant. At that point I should have then figured out the distance, the number of miles essentially, that light will travel in the number of days that was given in my days variable.
Now to actually show the user the output I’d like to put together something like a sentence that describes to them everything that’s been done here, all of the relevant data and the answer. Down in the body of my document I’m going to put in a set of PHP tags and in those PHP tags I pretty much want to put together a echo statement, or a series of echo statements. I could say echo In $days days, echo – let me put a comma there. I will say light will travel; I’ll put in my distance value. And I’ll do another echo and I’ll say miles. There we are. Let’s see how that looks when I run it in my browser. Uh-oh, looks like I’ve made a mistake in there.
Line 23, the error message is saying that I’m missing a semicolon. Line 23? Looks like I have a semicolon there, doesn’t it? What have I done wrong? Oh, I see I’ve missed the period I needed right there in order to append those two strings together. Let me try it again in the browser. There we go. It says In 7 days, light will travel big-gigantic-number of miles. Looks like it’s doing pretty well. If I come back over and I change the number of days – maybe I’ll make it 17 days – it recalculates based on the new value for days and it looks like everything’s working fine. One thing I can see here that I might want to do that I could use to clean this up a little bit is instead of doing this with three echo statements I could essentially do that with just one. What if I used string interpolation with the double quotes and I said In $days days, light will travel $distance miles.
Let me get rid of the three echoes I had down here below. I’ll take a look at that in the browser. Same output, but the actual code for it, I think, is quite a bit clearer using the string interpolation. I like it. So that’s pretty much all that was required in order to accomplish what I wanted in this example. The only thing I can think about it to point out is the separation that I put in between the two batches of PHP code. Essentially what I did was all of my pre-calculations, all of my working with the data, more or less everything that didn’t actually produce or need to produce any output I put into a set of PHP tags above the document and then the only PHP code I put inside within the HTML itself was the HT–the PHP code that actually needed to provide output.
I like this basic sort of set up. Of course HTML, CSS, JavaScript can get cluttered and busy all on their own; throwing a bunch of unnecessary PHP code into that part of the file just makes it that much more difficult to read and that much more difficult–er, that much more likely that you might have problems. So splitting the code up this way I think makes it a lot cleaner, a lot easier to read, a lot easier to debug..
No comments:
Post a Comment