Absolute Basics / Operators
Subtitles of the Movie
In this movie, we're going to learn about some basic ways of manipulating variables and we're going to introduce the concept of operators. First of all, let's look at the different kind of data that variables could hold. So far we've only seen string variables. That is variables containing a series of letters or numbers or symbols that are treated as simply a string of text. Now variables can also hold numeric data, such as this line of script, we define the variable num to the value of lets say 13 and now let's have a look at this line, the number 13 is introduced as the variables value, however in a way that's different from the example we used in the last movie. There are no quotation marks around the value. This tells php that the number 13 is to be treated as a numeric value rather then as a string, the characters 1 and 3. Let’s see how we use a variable like num. We can use the plus operator to add two numbers together or numeric variables or a combination of literal numbers and numeric variables. In this case, the number 7. Now let’s save that and we’ll see what that looks like in the browser. The number 20, so php has correctly interpreted the variable num as a number 13. We could also use two different variables to represent numbers. In this case the variable another and replace the number 7 by the variable name, this would do precisely the same as the last script. So that's pretty simple then, the plus operator. The other operators are similar to how you may imagine. The minus operator, there's also a multiply operator, it may look a little different from what youre used to in math's, its represented by an asterisk and there is a divide operator represented by a slash. The operators work pretty much as you may expect. However there are certain times when there may be some ambiguity in our minds but not in the computer. Take this expression, 4 plus 3 multiply 10, to us that may seem an ambiguous expression, which should we do first? Should we add 4 to 3 and then multiply the result 7 by 10 or should we do the multiplication first in which case we come up with a different figure 34. Let’s try it out and we’ll see which php does. So we go back to our browser and refresh. You’ll see php has opted to go for 34. Now the reason for that, even though the plus operator comes first is that php looks at the whole expression and it chooses to do the multiplication first because the multiplication operator has precedence over the plus or subtract operators. Similarly the divide operator has precedence over any plus or subtract operators. If we wished to make sure that the addition came first, what we could do is put in a pair of brackets. This would tell php that 4 plus 3 is to be treated as one smaller expression within the entire numeric expression that its being asked to echo to the browser. Let’s test that one out. We’ll go back to our browser and refresh the screen, this time we get a number 70. So we can see here that the rounded brackets are a good way of forcing php to carry out operators with lower precedence first. We’ll take a look in more detail at operator precedence later on but for now there’s another operator with which you may not be familiar and let’s take a quick look at that and see what it does. It's known as the modulus operator and it's displayed as a percent sign. What this does is it divides the first number by the second and returns the remainder, so in this case it's going to divide 12 by 5 and return the remainder 2. There is one final operator which we use very often in php and this is not a numeric operator, this is a string operator. if we use our name variable again and this time let’s say the name is Peter and we go down to our echo command and we’re going to echo the string ‘my name is’ followed by a concatenation operator and concatenation is just a fancy word for running two strings together. So we run first of all these set of letters here together with our variable name. Lets save that and the output, you’ll probably be able to guess my name is Peter. One final thing to say about the concatenation operator is that we can equally well concatenate numeric data such as 32, we use another concatenation operator, open quotes, make sure we leave a space after the quote so that's treated as part of that string and 'I am' you put in the age variable after another dot for a concatenation operator, another dot 'years old.' Make sure you include all the double quote signs that I used there. Once again we’ll save that and refresh the browser view and as we can see, php has treated the age variable just the same way as it’s treated the name variable and added it to the echo statement just as we requested.
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
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 