PHP Basics / General Output
Subtitles of the Movie
In this lesson, we're going to talk about general PHP output. Go ahead and open up a new document in your text editor. And start by writing some PHP code, such as echoing out some sample text. I'm going to display this in my browser window so I know that everything's working correctly. There is my sample text. Now I'm going to output some text again. This time I'm going to use the print command. Print some more sample text. I'm going to begin this text string with a HTML break tag so that we get a line return. As you can see, the print command works very similar to the echo command. It just sends the string or the text straight out to the browser. The difference between the echo and the print statement is that the print statement actually turns a value, a Boolean value based upon its execution. I could say that the variable result is equal to print and then some string. The output's the same, but I now have a Boolean value inside the result variable. I can test against this or use this in my programming logic to see whether or not my print was executed successfully. I'm going to go ahead and echo out another break tag and then I'm going to output the result variable. As you can see, the result is true, or a one. Because my print was executed successfully. The echo statement does not return such a result and that's the primary difference between these two output methods. Another method for outputting information to the browser is with the print underscore R function. Print underscore R is very handy when outputting objects and arrays because print underscore R will actually list out the contents of the arrays, whereas the regular print and echo statements won't. Because print underscore R is a function, you'll want to pass your string or whatever it is you're outputting inside the two parentheses. I'm going to output the result again, copy and paste my break tag in between them for easy viewing and you can see that the output or result came out just as it did before. Nothing special about the print underscore R when we're outputting regular text or integers. It's when we want to output the contents of arrays or objects that the print underscore R function becomes very helpful. If I use the echo or the print statements on an object or an array, it would simply just output the text array or object. But if I use print underscore R to output an array or an object, it will display all the contents of that object or that array. It's very helpful when you're debugging in your programming. There is another function that works a lot like print underscore R. It's called var dump. Var dump works a lot like print underscore R in that it outputs all the contents of complex things such as objects or arrays. But it also outputs metadata about each of the objects. I'm going to refresh the browser now so you can see what I'm talking about. Notice that print underscore R outputted just the value, whereas var dump outputted that this was an integer and then the value. If we were going to use var dump to output some text, it would output the text but also state that it was of type string and that it had eleven characters. This makes var dump a little bit more elegant when you're outputting complex items like objects and arrays. And it's why I use var dump most of the time. Now let's talk about comment in your code. There are three different ways to comment code in PHP. When you comment your code, you're basically telling the processor that you don't want to execute this particular line or lines of code. You can comment codes in a single line fashion by using two forward slashes. Notice that my code editor grayed that line out. When I refresh my browser, that line's omitted. Another way that I can comment out a single line of code is by putting in the dollar sign or the number sign. Again, the browser showed that that line was omitted from being executed. Now, if I wanted to comment out multiple lines of code, I could use the same characters we use in FileMaker's calculation engine. The forward slash and the asterisk. Then I can end this multi-line comment out by reversing the two characters. Something to be aware of in PHP when you're commenting out multiple lines of code, it doesn't play very well when you try to comment out groups of multi-lined code. What do I mean? Well, I've already commented out these lines of code. What happens if I wanted to comment out the greater group of that code? I'll go up here and start another multi-line comment and then go way down to the bottom and end the multi-line comment. Notice that even though the multi-line comment was ended down here, the echo statement above is not commented out. And that line will indeed execute. This lack of commentation from this multi-line comment is caused from the fact that nested multi-line comments in PHP doesn't work. Because PHP starts the multi-line comment but then stops at the first time that it sees the end of multi-line comments. It does not keep track of pairs. Using multi-line comments in PHP can cause things to become messy very quickly, especially on a large document where you'd use multi-line comments periodically. It's better to just comment out code line by line in PHP. Most PHP coders get into a habit of using two forward slashes for a group of lines to simulate a multi-line comment so that they avoid the nesting problem that you have with the multi-line commenting technique. This ends our discussion on general PHP output.
Tutorial Information
| Course: | FileMaker 9 & PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 33786 |
| ISBN: | 1-933736-99-2 |
| Release Date: | 2007-08-22 |
| Duration: | 9.5 hrs / 107 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 