Server Side Includes (SSI)
• In order to organize your work, and be a professional programmer, simplify your code. There are many things you could do, to organize your code, and facilitate development acceleration to your application.
• Here I’ll put some advices to you as a programmer that might help you in your job.
• First thing, simplify, and simplify… Make it easy. Your script should do only one job. This will facilitate handling errors.
• Second thing, it is not preferred that your function has more than three parameters to finish its job, this means that you can simplify it more.
• Use Loops only when needed.
• Don’t repeat your code.
• Divide your script in directories, and group codes that complete each other in the same directory.
• Use available (OPEN SOURCE Libraries) rather than build it yourself. This will save your time, and give your application more qualities. Remember “Don’t re-invent the wheels”.
• Finally, think of simplest and shortest ways to do your mission. Of course it should be legal methods.
Suppose that in your application, there is a menu that you need to be showed in more than one page. Of course, you have not to copy and paste the menu in every page. What about if you want to add a new item to your menu? Shall you add to all pages copying and pasting!! It is not a programming idea.
The best and logical thing is put your menu in a file, and call this file where needed.
How to accomplish this?
• Save your menu in a file.
• For example “menu.php”.
• At each page you need your menu to appear, call the file by using one of these two functions include() or require().
• Both functions are identical in their job, but, they differ in handling errors. include function will search for your file, if found it then will be included in your page, and so require. But if not found. If you used include, your script will continue working, and will not stop. Where require will show an error and your script completely will not work.
Example
<?php
require(“menu.php”);
include(“wrong.php”);
?>
• If menu is found, the script will continue working, but wrong file will not stop working your script.
• Where to use?
• If your script depends completely on the included file, then use require. Else use include.
• In my opinion, I prefer require.
End of Lesson 2.
To download this lesson as PDF click here: PHP Tutorial - Lesson 2: Introduction to PHP - Part III
Best Wishes,
Anas Jaghoub
Comments
Post a Comment