Absolute Basics / Embedding PHP within HTML
Subtitles of the Movie
In this chapter we're going to look at the very basics of PHP. We're going to take you through your first steps in writing a PHP script. First of all, we're going to look at the way PHP scripts are embedded within html so they can be served from the server and passed to the users browser. Then we're going to look at how we declare variables to make our script more flexible and we're going to look at ways to manipulate those variables using operators and finally we're going to take a look at some more complex operators and do some more complicated things with our variables. So first of all, let's open up our text editor and we're going to start with our very first PHP script. We use a PHP open tag to define where our PHP script starts and stops. This is very important and this example will show you why. To start with, let's write a plain html page and then we're going to try and put some PHP into the body of the page and see what happens. Let's use that line of code that we used in our example at the end of the last chapter for our hello world script. Now you may spot there is something wrong with this, so let's see what happens if we try to save that to our document root, which we find in this case in Apache, because that's my local web server under htdocs, I'm going to put this one in examples and we're going to call this test.PHP We save that and now let's open up our browser and we'll open this using the local host path. Now we saw some of the reasons for that in our last chapter but it's very important that we use the local host path and not the absolute path via the disc. So what do we have here? Something is definitely wrong with this, there's the hello world text that we wanted but here is this echo command showing up and the quotes around the hello world and this semi colon at the end. Now we didn't want any of that to show up, all we wanted to show was hello world. So what is the reason that this has acted like this, when the last time we tried it, our script worked. The reason is that we've forgotten to put in our PHP tags and this is why these are so important. If you put in your PHP tags and whatever it is you write between them is evaluated as PHP code, anything else is evaluated as html. So we could put after that, this is just html and we're going to put in a little break there. Now let's save our script, go back to our browser and we're going to hit refresh. As we can see, this time all those ugly parts of the code we didn't want the user to see are gone and this text is showing up just the way it should do. That's because this was outside these PHP tags whereas this was inside the PHP tags. So it evaluated first of all the echo command and then what the echo command does is echoes any string to the browser, the string is a series of letters and numbers and symbols, whatever you like, it treats them as alpha numeric characters. In this case, its hello world, the PHP knows where the string starts and stops. We use these little double quote signs to demarcate the strings. Then at the end, we put a semicolon. The reason for this is that it may not seem very important in this script because it's only one line. But as your PHP scripts get more complicated, you're going to writing scripts more and more lines and each line represents a command. PHP needs a way to differentiate between the commands, so at the end of every line, we put a semi colon that splits a line. So far all our PHP script has done is nothing that we couldn't do just with html. Instead of writing echo hello world, we could have just written hello world in here and saved ourselves the trouble of all of this. So in our next movie, we're going to look at variables and this is where PHP will start to get more interesting, more flexible and more useful for us as server side scripters.
Tutorial Information
| Course: | PHP |
| Author: | Joshua Mostafa |
| SKU: | 33332 |
| ISBN: | 1889347787 |
| Release Date: | 2002-03-26 |
| Duration: | 9.5 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 70,000 Video Tutorials (14,250 free)
- Video Available as Flash or QuickTime
- Over 650 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 