Absolute Basics / Variables
Subtitles of the Movie
[00:00:00.0] Variables are one of the most basic concepts in programming. [00:00:06.0] A variable allows us to give a name to a container for data without explicitly referring to what that data is. [00:00:15.0] Let's take a look at an example. Let's start our PHP script with the open PHP tag and then let's define our first variable. [00:00:28.0] Let's call our variable 'name', now in PHP unlike other programming languages, we can define a variable and set it's value in the same line. [00:00:42.0] You'll notice that each variable begins with a dollar sign. This makes it very easy for us to pick out our variables as we look through our PHP script. [00:00:54.0] To assign a value to our variables, we use this sign. This is sometimes called the equal sign but in PHP we're going to call this ‘is set to'. [00:01:07.0] So the variable name ‘is set to' and then we give it a value. So in this case, let's give it the value Joseph and put that inside the double quotes to show that [00:01:25.0] this item of data is a string, which means any set of letters or numbers, then we close with a semicolon to show that,that's one line of PHP. [00:01:39.0] Now we're going to try to make sure that, that variable has taken it's value as we intended. [00:01:47.0] So let's echo that variable to the browser, we type in echo name, always remembering the dollar sign that precedes every variable in PHP, [00:02:01.0] another semi colon and we close the PHP tags. We're not using the html tags and the body tags that we would normally use. [00:02:12.0] This is purely to save time, when you come to make your PHP scripts, you generally find that you want to use all html tags so that [00:02:22.0] your documents are formatted as valid html. Just to save time, we're going to leave that out for now. [00:02:29.0] So let's save this as we did before to our document root and we're going to put that in examples and this one we're going to call testvar.PHP. [00:02:52.0] just save that, now let's take a look at what that looks like in a browser. Once again we type in the address using local host and let's see what that comes up with. [00:03:18.0] It's echoed the variable Joseph to the browser. Now if we look back at the code, what it's echoed is not name, it's echoed the contents of that variable, the value. [00:03:33.0] So name is the variable in this case and Joseph is the value. Now we can change the value of a variable anytime. Let's try that. [00:03:47.0] Let's add another line and we're going to use the same format. We're going to type dollar name for our variable and this time [00:03:58.0] let's say we're going to call this name Mary and then we're going to type exactly the same line. [00:04:13.0] Let's save that and we'll go back to our browser and we'll view it again by refreshing the screen. [00:04:27.0] Now what's happened here, we have Joseph followed by Mary. We look back at script, the reason for that, even though these two lines are identical [00:04:40.0] echo name and echo name, the reason that two different names are output is that every time we set the value of a variable [00:04:49.0] that dollar name stands for something different. So we can see that by using variables we can make our scripts more effective and starting to look a little dynamic. [00:05:04.0] One final thing about variables that I'd like to point out is that you can use whatever name for a variable you want in your scripts. [00:05:14.0] You can use a really long name and PHP will still correctly interpret it. There is nothing wrong with using a name like this for instance if you wish to. [00:05:40.0] Of course, it makes more sense to use names for your variables that when you look back in your scripts, you're going to know what they mean. [00:05:51.0] So the name variable is probably a better name than the supercalofragelicstic xpllodecious variable. [00:06:00.0] If you want to be able to look back at your scripts and understand what you have written or even more so if anyone else is going to be looking at your scripts [00:06:09.0] and trying to work out what you meant when you wrote them. However you're free to use whatever variable names you wish. [00:06:22.0]
Tutorial Information
| Title: | 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 