![]() ![]() $my_var += 5 // my_var is defined in index.phpĮcho "initial value of my_var: ".$my_var."" Īs we can see, the variable $ my_var that was defined in index.php can be accessed (and modified) from our external file. My_file.php (external file that we’ll include in index.php) " ![]() Now let us look at an example where we can access and modify the state of our main code from an external file upon including it. variables declared in the external file are also accessible in your main code. Therefore, the variables, classes and functions accessible in the main file (where include is called) are also accessible in the external file. Īs you can see, the code from the included file ( my_file.php ) has been executed at the line where the file has been included.Īnother thing to note here is that when you include an external file in your code, the variable scope of your main code (at the line where the include function is called) is merged with that of the included (external) file. Now we’ll include this file in our index.php file. We’ll do this by creating a file my_file.php and including it in an index.php file. ![]() Here, we’ll see how the code of an external file is executed from our main file upon inclusion. We’ll start with a fairly basic example. Now let’s look at some examples that use the include function. When we import an external file into our code using the include function, the statements of the external file are executed at the line where include is called. You can invoke the function using either of the two ways. The include function takes as an argument the name of the file (string) to be included. Note: Throughout the rest of this post, the external file refers to the file that is to be included, and the main file refers to the file that is including the former. Let us look at both these functions in more detail. This also means that components created once can then be reused anywhere across our application, making it easier to maintain. Like we discussed above, being able to include files into our code allows us to systematically modularize and organize our project - to create separate files for separate components of our application and pull all of them into our code, wherever required. This is done by copying the contents from the external file into our main file during run-time. Let’s get started! Include and Require Functions in PHPīoth, include and require functions in PHP that are used to import external files/modules into our code and evaluate them at the place where they have been imported.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |