|
just another regularban.info web blog |
| MEMBERS: | Reusing Code in Web Sites and Applications Part 1 - Using Include() and Require()
Reusing pieces of HTML code is a common way of making web sites (or web applications) more consistent, reliable, and more manageable. Even a small web site consisting of perhaps 20 or 30 web pages can benefit from reusing code for header and footer sections, for example. If the same header or footer is used across all the pages on the site, it makes sense to put the header and footer code in separate files, and then call those files to insert their contents where required. PHP offers two statements that can be used for inserted the contents of one file inside another: include() and require(). These two statements are virtually identical, with the only real difference being that if require() fails, it gives a fatal error, whereas if include() fails it just gives a warning. For this reason I tend to use the include() statement as it just seems slightly more friendly. So, to include the contents of a file called header.inc inside a web page called index.php, you would just need to insert the following line of code (wrapped in PHP opening and closing tags) inside index.php. Include 'header.inc'; As PHP takes no notice of the extension used for the included file, you can use whatever extension you want to. Be careful though, if you put passwords inside an include file with a .inc extension, they will be visible. You can get around this by putting include files that contain sensitive information outside the document tree to prevent people from browsing to them. In Part 2 we'll look at the use of functions when developing web sites and applications.
How To Create A Simple, Effective Website
Before you build any website it is important to sit back and think about exactly what you want to achieve. This will help you to stay focused on your objective. Now I am going to presume that you want your site to make you some money. Wile it is nice to make money if this is your only objective you may face many challenges ahead. The reason is because you may find the level of motivation may dwindle if you do not start making money quickly. What I want to emphasize is simplicity. Keep it simple. Do not over complicate the process. To create and effective website you need to understand why people use the internet. Our main goal when we go online is to get information for one reason or another. This may be for research or entertainment purposes. However the premise remains the same. One way or another your site will need to provide content relevant to the individuals search. This content can be provided by audio, video or written means. The fact is that it has to be relevant in order to be effective. We all know how we feel when we find what we are looking for. This builds credibility and as a result the recommendations you make will be followed which means you will make money. The bottom line is that we all want results and desire to achieve a certain outcome. You need to know what your visitor's wants and needs are then address it and provide solutions. Communicate with your visitors in a way that makes sense to them and provide logical steps that they can follow. As for the technical aspects of building a site the good news is that there are many website building software tools out there that can help with this even if you have no technical knowledge. So don't sweat the small stuff.
Using Functions
Functions are used to perform a well defined task that is normally repeated at various places within a web site, web application, or other software application. The function sometimes needs certain information before performing its task, and sometimes returns a value to the calling page or program. An example of how to use a simple function in PHP: function showMessage() {The above example shows a very simple function that displays the contents of a session variable called 'message'. The idea being that while a user is using a web site, etc, various messages are generated and stored in the 'message' session variable. Whenever the showMessage function is called, the contents of the session variable are displayed. So, for example, when the user logs on, you could display a message saying that they have done so. Likewise, when they log off, a different message could be displayed. Calling a function To call the showMessage function, you would just need to include the line: showMessage() in the web page (wrapped in php tags to distinguish it from normall HTML). Passing values to a function Often, a function requires one or more input values in order to perform its task. For example, take the following example that takes two numbers as input, and adds them together. The result is then returned to the calling web page or program. function addNumbers($number1,$number2) {In the calling program we would have something similar to the following: $number1 = "5"; Obviously, in a real program you would not have the numbers hardcoded like this, but they would be obtained from user input or by some other means. Notice also that although the name of the returned variable is $answer, the calling program makes no reference to that. We could, if we wanted to, change the calling program to: $answer = addNumbers($number1,$number2); which might make things slightly easier to understand.
|
* About Archives
Categories:
Last Updated: |
| regularban.info
is proudly powered by WordPress MU running
on regularban.info.
Create a new blog and join in the fun! Entries (RSS) and Comments (RSS). |