Hello! One thing you will hear everywhere you go as a coder is code reuse – the idea that if someone has already wrote some code that you need, you might want to consider using it instead of spending so much time writing from scratch. You could choose to repeat the process but your boss might not like it very much!
So today, am going to start on code reuse. There are several advantages to code reuse and they include: reduces the cost (you will spend more time maintaining your code than writing it, therefore you want to make sure that you limit the number of lines in use.). In simple terms, less code means lower cost. Secondly, there is reliability – existing mature code is always more reliable than a fresh ‘green’ code. Thirdly, consistency – If you are reusing code that runs in another part of the system, your functionality should automatically be consistent.
Using require() and include()
PHP provides two very simple, yet very useful, statements to allow you to reuse any type of code. Using require() or include() enables you to load a file into your PHP file. The imported file can contain anything you would type in a script including php statements, text, HTML tags, PHP functions, or PHP classes. So what is the difference between the two statements?
Good question there. They are almost identical except for one thing: when they fail, require() construct gives a fatal error, while include() construct gives only a warning.
One more thing to mention here is that there are two variations to require() and include(): require_once() and include_once() and you might have guessed what they mean? They mean that the respective files can only be included or required once! Consider a situation where you have included a file twice without knowing, you will get an error. So, if you are cautious, you might want to try require() or include() because they are faster to execute.
Consider a file named: reusablefile.php
<?php echo 'Hello there, this is a reusable code <br />'; ?>
And now consider another file named: main.php
<?php echo 'This is the main file for testing stuff <br />'; require('reusablefile.php'); echo 'This is where the script ends at this moment <br />'; ?>
Now the outcome: Running the main.php file will return the page shown:
How easy was that? So, it is as simple as using an echo statement in between the two lines.
Using require() for website templates
You can use require() while creating a website that has a consistent look and feel. If you want the visitors to see the same header and footer throughout the pages, you simply require those templates in every page. If you want to add new pages, you do the same thing as before, require the header and footer. The only thing that could be different is the content!
So, simply create a header and store it in a file like ‘header.php’ and create another file ‘footer.php’ and then create another file ‘home.php’ and require these two files inside that file! Consider a file home.php
<?php require('header.php'); ?> <p>Welcome to our crazy website where real problems get solved, and tough questions answered!</p> <?php require('footer.php'); ?>
It is a convention to name your files that you plan to include in other files with a .inc extension (includes). This is not recommended because .inc extensions are not interpreted as php unless the server is configured to do that.
Another alternative is to use auto_prepend_file and auto_append_file. By setting these two properties to point to the header.php and footer.php, you ensure that they will be loaded before and after the page loads! If you use this directives, you will no longer need to use include() on your pages but the headers and footer files will not be optional.
That is good enough for today! I figured I should get those out of the way before moving on to defining and using functions in PHP! Stay tuned and see you tomorrow. Thanks for stopping by and I hope this post helped you learn something. If you see any errors, please notify me so I can fix them.