PHP Includes

on Sat Jul 05 08:38:12 GMT 2008 in PHP and viewed 3039 times

One of the most basic and useful things in PHP is includes. You can use them for just about anything, and they carry many uses.


Ok, today we’re learning how to use simple PHP includes. They’re useful. I used to use them for almost everything in my site. Ok, so lets get into the code.


  <?php
  include("yourfile.txt");
  ?>

That’s it. Easy, eh? That’ll just include whatever is in the yourfile.txt on your server. It can be any type of file. I use .php. Also, you can use variables. So like where I use in my urls index.php?id=tutorials, you can do the same thing. This way I don’t have to make new pages for everything. It’s just index.php with includes. So for my code, it’s:


 <?php

 if (!isset($id)){
 include("news.php");}
 else{
 include("$id.php");}

 ?>

See? Simple. So all my index.php?id=tutorials page is doing, is it requests the index.php page. Then it says the variable id is equal to tutorials. So now, the statement includes tutorials.php in the content place, or $id.php. So now, hack away. Have fun with my first tutorial.