PDA

View Full Version : Basic Usage of include


eRage
11-11-2008, 03:38 PM
Today we will be learning about the include function in the coding language of PHP. Lets begin by creating our php document.


<?php

?>


Now, we will add the includes function. In this example we will be including a text file named hello.txt. Inside hello.txt there is a paragraph or lorem ipsum.


<?
include "hello.txt"
?>


Output:

Proin quis elit ut risus rutrum imperdiet. Integer arcu nulla, pharetra condimentum, interdum quis, laoreet in, diam. Nulla interdum dignissim magna. Integer vel ante sit amet velit facilisis aliquam. Donec gravida gravida elit. Nullam non nisl. Suspendisse nec nisl vel arcu semper sollicitudin. Vestibulum luctus pede a velit. Vivamus vel eros. Nullam hendrerit nunc at orci. Cras vestibulum, arcu id placerat commodo, nunc ligula placerat risus, non mattis justo libero nec metus. Maecenas sed pede. Sed urna.


The includes function is regularly used in websites to include things that may change in the near future, such as news, navigation, banner etc. Personally, I use it to include all content, navigation and adverts on my websites. A more common use of the includes function is used in many large scripts, such as forum software. Here is a basic example of how it may be used.

hello.php (the file we will be including)

<?php
$name = "jack";
?>


index.php (the file we will be using the include function on)

<?php
include "hello.php";
echo "hello $name";
?>


output:

hello jack


You can use this function to include variables, as shown above, database connections etc; the possibilities are endless.
Good luck.