Files Including in PHP

One of the most popular features of PHP is the ability to include files, it allows you to separate your code into multiple files, and re-use them in different places. For example, you can create a file with the function you use, and then it may be necessary when it is included, or you keep your static markup in one place for easy updating of your site You can use it for It may look a bit abstract now, so let's start with a simple example:

Syntax- 

<i>Below this, content will come from another file!</i>
<br /><br /><br />
<?php
$number1 = 20;
include("includedfile.php");
?>
<br /><br /><br />
<i>We're back, after the included file has ended!</i>


<b>This text is included!</b><br />
<?php 
$number2 = 30;
echo "And so is this PHP code - ".$number1." + ".$number2." equals " . ($number1 + $number2) . "!";
?>

Including files based on the query string


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>My website</title>
    <meta name="generator" content="TSW phpCoder 2008" />
</head>
<body>
This part of the website is static. Dynamic content below!
<br /><br /><br />
<?php
if(isset($_GET["show"]))
{
    switch($_GET["show"])
    {
        case "about":
            include("subpage_about.php");
            break;
        case "contact":
            include("subpage_contact.php");
            break;
        default:
            include("subpage_index.php");
            break;                
    }
}
else
    include("subpage_index.php");?>
</body>
</html>



<?php
include("top.php");
?>
The actual content of the page goes here!
<?php
include("bottom.php");
?>



Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment