Placeholder Image

Subtitles section Play video

  • So let's talk about the print function in PHP and the New Line Command.

  • As I've thought about before.

  • Many people use PHP essentially as their secretary for the Web pages.

  • PHP will take information from Web forms and dump that information into databases or files, And it can read from databases and files either out port output to a Web page or output to something like a text file.

  • So being ableto print from databases being able to print from PHP scripts is a very important thing now.

  • It is important, understand, when you print, you have to be thinking about what you're printing to and what the user interfaces for the person that's going to be consuming the content that you're presenting.

  • So if you're printing out a report, if you open up a file and a text editor, it is going to look different than if you open up a file in a Web browser.

  • So if you're going to be out expecting your viewers to read content in a Web browser, you have to remember to format everything in HTML.

  • Whereas if you expect people to read something, let's say in a log file or if they're going to be printing things out.

  • May possibly you need to format everything so that it looks appropriate in a text editor or something like word.

  • So it is very important.

  • Understand, when you're using print on when you're using the New line command that you really think about how the person that is going to be reading the content is going to be reading the content again, whether it's going to be from the Web browser or whether it's going through something like a text editor or word processor.

  • Thea Other thing that you have to think about when you're using PHP in orderto dynamically create Web pages is if you have to go and look at the source code that is dynamically created.

  • So whenever you look at a Web page, there's the Web page, you see.

  • So if you go to CNN dot com or fail normal dot com or line the computer guy dot com, you're going to go there.

  • You're going to see pictures.

  • You're going to see text and fonts and colors, and basically you're going to see a website how you're supposed to see it.

  • But remember, your Web browser is reading HTML and Java script code.

  • And so you can actually inspect that code.

  • See, right, click and go to to inspect.

  • And when you do that, you could actually see all the underlying code all of the HTML, all of the Java script, all of that kind of stuff.

  • So if it's also important to think about when you're learning, PHP is to think about how to format the output of your code in such a way that if there are errors, you can go and you can inspect the code.

  • And it's easier for you to see what's going on in the back end so that let's go over to the computer and I can show you how print and the new line command works when you're printing out when you're using PHP.

  • So here's a simple script using the print of function in PHP, we have three different examples here.

  • The first example is, was where were simply using print.

  • The second example is we're using print, but we're also using the New Line command at the end of every sentence and in the final one is where I show you how you can use New Line kind of be stupid if you want to, just to realise what extreme you can go to.

  • So basically up here, we're taking a look at this and we have the title for this particular example.

  • So we have a tch one.

  • This is just an HTML print PHP print example.

  • Then below this, what we're going to do is we're going to call the PHP script and then we're going to use the print function.

  • So to use a print function, all you do is type NPR.

  • I, Auntie space double quotation marks.

  • And then you say whatever it is you want to say, So here we have hyphen, print item one.

  • Then we do it again.

  • Hyphen print item to hyphen Print item three Basically, you can plug anything in here.

  • I'll show you in the future how to do things such as plug variables in here.

  • Ah, you then close with the double quotation marks and for every line, you then finish it with a semi colon.

  • It's very important to remember the semi colon.

  • So what this is going to do is is going to print print Item one is going to print print item two and then it's going to print print item three.

  • And so it's important when you take a look at this with how this is on the page, how I wrote this code, you want to think about how you think this is going to be printed out, both an html in and in text.

  • We'll show you the example on a minute.

  • Once I go through the other code that we have here, we then come down here for this particular page.

  • We have H one PHP print with the New Line command example, and so again we call PHP.

  • Then we're going to do print.

  • This is identical to what we have up here on Lee at the end of each sentence.

  • Before this, the quotation marks I did back slash in.

  • So with this, what's going to happen is going to print that is going to print item one.

  • Then it's going to say Go to new line and then it's gonna close.

  • Semi Colon, so is going to print this on one line.

  • Then it's going to print item to using that backslash in on another line, then you're going to go down to print Item three ends backslash in.

  • It'll print that on the next line.

  • So using the backslash end, this is how you can print onto multiple lines.

  • So instead of having everything just get printed across on one continuous line, using the end as how you can break and you can go to the next line to show you a ridiculous example of this we have the H one stupid new line command ops, stupid new line command example.

  • And what you have down here is again we call PHP.

  • We then do print space, double quotation, and then, well, you can see here is basically after every letter I put that new line command in so that it will go to the next line in next line.

  • The next line, the next line, the next line, the next line, the next line.

  • So this is the ridiculous example.

  • But this is basically the two ways that you use the print function.

  • Either you can print all in one line so you can print different things that all of one line or you can use the New Line command to go to new lines.

  • Now, when we go and we take a look at the Web page.

  • It gets printed out from this.

  • You might be surprised with what you see.

  • So when we upload this, if you go to silicon, does show printed dot PHP, you'll see the example.

  • And you might be confused here because you see the PHP print example H one and then you see print item one print item to print item three and these are all in the same the same row, so that probably makes sense to you.

  • But then you come down here and you see the pH people print with backslash an example, and you're sitting there.

  • You're probably thinking, Well, isn't this supposed to be a new line?

  • But again, everything is everything is in one in one line.

  • Why is that?

  • And then again, if we go down here to this stupid new line example, you can still see everything is in one line.

  • The reason for this is remember when you're using PHP, it prints out as a text file.

  • And then if you want to use any formatting, you have to make sure that you also print the HTML tags that go along with it.

  • So if we go here, we go down and we take a look at the view page source.

  • What we can see when we look at the text of this file is you're seeing something entirely different than when you're looking at the file through a Web browser.

  • So here, right print item one print item to print item three.

  • This is all in a row, as you would have expected from the code.

  • All right, so print item one print item to print item three.

  • You see that all from the code that makes sense.

  • Now, when you come down here for the new Line command, what you can see is print item one was printed on one line, then print item two was printed on the next line.

  • Then print item three was printed on the next line.

  • So although you don't see this using a Web browser, this is how it looks when you see a text editor, Then when we come down when we take a look at the stupid example, that's when you get this stupid mess down here.

  • Basically again, we printed each individual letter on a different line, even though when we go here, we don't see the exact same thing.

  • So this is the important thing to understand when you're using print and when you're using the new line command, what you print out toe a text file is not necessarily what the Web browser will see.

  • So you really have to think about that.

  • So with all you have to dio in order to use the print command is again you you call the PHP.

  • So you have the PHP tag you do print.

  • You do space, you do double quotation marks, and then you say whatever it is you have to say, and then you close double quotation marks and then you end the line with a semi colon.

  • You keep doing that or whatever else until you're finished with that bit of PHP code, and then you close the PHP tag.

  • If you want to do the new line again, you're gonna be writing out to different lines with text again.

  • The same thing you call the PHP with pH be tag.

  • You use print use the same function.

  • You don't use print Ln That's something you use for other coding languages Again.

  • You do space than you do Double quotation.

  • You say whatever it is, you want to say, and then wherever it is, you want to create that new line.

  • You drop in backslash in that will create a new line, and then you can close the double quotation marks and you can do the parentheses.

  • So with that, that's how you use the function command and how you use the new line.

  • Do you realize when you print this out to, uh, to your text file how it's going to be viewed in a Web page is different and how it's going to be viewed as a text editor?

  • So that's all there is to using the print function and PHP and the new Line command.

  • You just do print space, double quotation marks, say whatever it is you want to say, Close double quotation marks and the dew is semi colon.

  • If you want to put a new line within the print function, you just do backslash end wherever you want that new line, and then it will print everything out in the text file as the basic text.

  • It is important to remember, though, when you're out putting using PHP, you have to think about how your users or how your viewers are going to be consuming the information.

  • Are they going to be consuming it with a Web browser Or they're going to be consuming it with a text editor?

  • Or were processor that will determine whether or not you're going to be adding in additional HTML tags or whether you strip them out?

  • So this is an important thing to be thinking about.

  • It's important to think like how somebody is consuming the information and what user interface there they're using to consume the information for thinking about how you're going, be writing the code.

  • The other thing that's important, especially using the new line command, is when you're beginning to write code.

  • It can be very confusing to figure out where your problems are.

  • So when you look at something on an HTML Web page, ah and you see an error somewhere.

  • If you then go to the source code and you look at the source and all you have is one big block of 30 lines of source and everything's mixed in and may be very difficult to troubleshoot and figure out where the problems are.

  • So if you view code your PHP script in such a way that it outputs a source that's easy to read that will make your life a lot easier, especially in the beginning, when you're trying to troubleshoot your code again.

  • When you start adding functions, when you start doing things that are more complicated, if all you have is one big block of source that gets out, put it by PHP.

  • That could be very confusing to look at.

  • If you have something that's formatted relatively decently, you can go and you can say, Oh, okay, this is the section where I know I'm having a problem.

  • Oh, I see I did something wrong there.

  • I forgot to put a semi colon or whatever There I see.

  • That's what the issue is.

  • So that's how you use the print function and the new line command and why it matters.

So let's talk about the print function in PHP and the New Line Command.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it