We will be undergoing scheduled maintenance on May 20th, 2013 at 02:00 GMT.
Let's go ahead and look a little bit more about outputting text with PHP. As a proficient web developer using PHP part of your goal is to output dynamic web pages. Pages that are dynamic based upon conditions. We're going to look at three of the key ways to output text in PHP. The first way is going to be using double quotes and in this instance the double quotes delineate the text. So we have a delimiter right here at the beginning and at the end. And if we open this up in our browser, as you would expect, you'll see that text has been outputted to the browser. Now what's really nice about double quotes is that it does honor variable substitution. So that if I said X is equal to, is really cool then I could go ahead and put X inside my double quotes and it would go ahead and output the value of X to the browser. Another way to output text is to go ahead and use single quotes. What's nice about single quotes is that you can have double quotes inside of it and you don't have to worry about it breaking your string. So I've just enclosed this whole line of text in single quotes and you can see that it's outputting everything I wrote inside of that. But what you'll also notice is that we are no longer getting variable substitution. It's outputting exactly what I wrote which was the dollar sign and the variable sign, the variable. The reason is why is that single quotes do not honor variable substitution. So you'll have to remember that when you're coding that if you want to put a variable directly into your code and have it merged, you'll want to use double quotes. If I was using single quotes here then I would need to use the period to concatenate that variable inside of my text. So I would use the delimiter to stop the text which is a single quote and then put a period and the dollar sign and another period and then start up my text again with another single quote. One more difference between single and double quotes is the fact that you can have special characters in the double quotes. For instance a backslash and a N is a new line. You can get a list of the special characters such as tab and return and so forth on the PHP documentation website. I'll save this and I'll refresh it in my browser. Now the browser is going to interpret HTML formatting the line characters. But if I look at the source code you can see that we have a line return embedded here inside the source code. So those are the two big differences between using double quotes and single quotes and one of the things that may not readily be apparent is the fact that when you're outputting HTML such as an image tag. If I were to output IMG and then the source of the image tag which would have to be in double quotes, you can see that I've got some bad coding going on here. Because I need to enclose my image tag in double quotes, I don't want to use those for my actual text that's going to be output to the browser. So understanding that if you're going to be outputting code such as JavaScript or HTML that contains quote characters, you'll want to think about that when deciding what your delimiters going to be. And when I go ahead and refresh this you can see I'm getting my output. Now the third way is for you to be able to pick your own delimiter, neither a single or a double quote. Then you can output any number of characters inside of it without breaking the string. And how you do that is with the Heredoc syntax. So Heredoc is spelled H-E-R-E-D-O-C. The Heredoc construct has a few simple rules to it and I'll go over those right now. First of all we'll say that our text, I'm using a variable here is equal to and here's where you get to choose your delimiter. You use three less than symbols and then any number of pieces of text. So you could just use the word text or you could actually put your name or you could put something else but we'll stick with text. Then your ending delimiter needs to be on it's own line and it needs to be the exact same delimiter that you used at the beginning. And there needs to be no spaces to the left of it. Then you use a semi-colon as you would normally in PHP to terminate that line. Now you can put any number of pieces of text inside of here and PHP is going to consider that to be all text. In fact I could take all my code here and put it inside here and you can see that it's accepting it all as just plain text. And now all I need to do is output my variable text and you can see that it's outputting everything that I wrote right to the browser. So that's a summary of how you output text using single and double quote delimiters. And then using the Heredoc syntax.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |