|In a previous tutorial, we created the Activity_Entry_Form ( ) function that will print the Activity Entry Form for our Personal Activities Calendar Program. We did this because this section of our code will be used several times in the program. There is another section of code that will be used more than once in our application and this is the code that checks for missing data after the form has been submitted. Because this task is performed several times, we can simplify our program by separating out this section of code as we did with the function. There are other ways to compartmentalize sections of code, for example, you can create a PHP class or simply use the PHP include( ) statement. Because this is a beginner's tutorial, let's discuss the include( ) statement method. |
What the PHP include( ) statement says is "get this file and place it right here". Therefore, you will want to place the include( ) statement within your program at the exact stop where you want the included code to be executed.
Let's go back to the ActivityEntry.php3 file and separate out the code that checks for missing data. Now cut and paste that code into a blank file and name that file CheckData.php3. If you take a look at the sample, you will notice that I have not only pasted the section of code from the AcitvityEntry,php3 file but I have also added an opening and closing PHP tag. If I had not added these tags in our new CheckData.php3 file, the code would have been treated as plain text instead of PHP code.
Now all you need to do is put the include statement into the ActivityEntry.php3 file at the place where you removed the code previously.
You will want to put the relative path to the file to be included (CheckData.php3) between the quotation marks. This is called an argument for the include statement. You will also notice that, unlike the function code example, we do not need to use a require_once statement at the top of the file.