Placeholder Image

字幕表 動画を再生する

  • So let's talk about using include and PHP.

  • So what if there's code that you want to reuse?

  • So let's say you create some kind of header or you create some kind of footer.

  • Or basically, you create a number of variables, and you want to be able to reuse them on different scripts within the application that you're gonna be using.

  • Well, one of the things that you can do is you can use include to allow you to include other PHP scripts within the PHP script that you're that you're building so you what you can do is you can create a basically can create a PHP script where all you do is create and set variables.

  • So here are all the variables.

  • That's all that is.

  • And then when you create a different script that has a functions or has a form or has something else, and it needs to be a bit, call those variables.

  • One of things that you can do is you can simply include that original script that you created, and now it's as if all of those variables have been written in to the PHP script that you're currently dealing with so This is a simple way again on some people.

  • There's an argument about whether you should be using PHP for creating templates and all that kind of stuff, and nowadays, but this is one of those ways that you can do things such as create templates.

  • So if you create something like a title bar, if you create a menu or create a foot or something like that and PHP and you want that to show up, ah, multiple different pages, you can write that out once, and then you could simply use include to include that script onto whatever pages you wanted to be included on.

  • This is a simple way that you can do code reuse whether or not it's the best way all the time again, especially in a corporate world.

  • That's its own question.

  • But this is something that you can use, especially as you're learning.

  • Let's go over to the computer so I can show you how to use this.

  • Include, So this is a simple include script that I created, and again it has a bit of a little bit of a troubleshooting routine, and here, just to make sure things were firing properly, um so we've called this, include, so this is just called include a PHP.

  • And with this, as always, you open with a PHP script, and then what I'm going to do here is I'm going to create a variable for time and set the value to whatever the current computer time is.

  • All this is for is for troubleshooting purposes only.

  • This isn't actually for the include.

  • Then what we're going to do is we're going to include a PHP file.

  • And so, of course, just be a little bit confusing.

  • I don't know why I got into this.

  • This is probably not a good idea.

  • I called it included.

  • PHP.

  • So we're in include that PHP and we're going to do is we're going to include to be clear, single quotation marks, single quotation marks, and then what?

  • The path to what we're including this s o.

  • Since this is in the same directory, all we have to do is tell it its name.

  • So it's gonna be called included dot PHP close single quotation marks on, of course.

  • And with a semi colon.

  • Then what we're going to do is we're going to print and we're going to print the value of the variable message.

  • And what you'll see here is that I have not created nor if I set a value to a variable called message anywhere else in the script.

  • This is have been created in this included PHP script.

  • Then all I'm going to do is I'm gonna print a break and then I'm going to print the time and again, this is just going to show me that the script is actually firing how it's supposed to.

  • And so if we go over and we take a look at the included file, what we're going to see is for this included.

  • So any any PHP script that you're on be, including, has to be a PHP script.

  • So this is included, not PHP.

  • We're going to open with a PHP tag, just like we normally would.

  • And all I'm doing here is I'm creating the variable called message and setting it, setting it to the value of hello world from an include again close with semi colon and then close the PHP script.

  • So it's important that you have the PHP open and the pH be closed here, and if we go back to the include so we can see that this is this is how it's gonna work.

  • So the print So the value for the message is coming from that included.

  • We go thio chrome to take a look at this.

  • We can't go to silicon dough, Joe.

  • Forward slash and again.

  • Include.

  • Make sure you just do include a PHP.

  • Ah, you may.

  • You may.

  • You may want to change the name of that file.

  • That was probably not a good is probably to get thing anyways.

  • Okay.

  • And so here we go.

  • So it prints out Hello world from an Include, So that's coming from the included file.

  • Ah, and then this is just the time and again the time is there just to show me that the script is actually running.

  • Um and so that's really all there is to how you include Ah, one PHP script into another again.

  • You can use this for things like variables.

  • Any kind of code reuse you're creating forms of you're creating reports of you creating variables and you just know you're going to use it again and again and again, you might as well create create that information right out out in one place and then you can use include in order, pull it in for the different scripts that are going to use it.

  • And so that's That's really all there is to include script and again, that's that's really that's That's basically what they look like.

  • So it's all there is to using include and PHP just a very easy way that you can reuse code, especially when you're new.

  • When you're testing things out, you know, you might as well write something once included in a number of different pages.

  • Ah, and then work things out from there.

  • There's no reason to keep writing things.

  • One of the big problems you run into in the coding world is simply people miss type things.

  • So if you have the same, if you have the same code and you're gonna have to re type it five different times, most likely you're gonna make a couple of mistakes again.

  • The more mistakes you make, the more frustrating it's gonna be, the more gonna scratch your head, whereas if you just simply write at once, you make sure that it works that one time, Then you could just include it on all the other scripts that you're creating makes a life a lot easier.

  • So that's all there is to using include and PHP.

  • That's why it matters.

So let's talk about using include and PHP.


ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

PHP - コードの再利用 - インクルード (PHP - Code Reuse - include)

  • 0 0
    林宜悉 に公開 2021 年 01 月 14 日