PHP: Include and Require

5 posts Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

PHP: Include and Require
smashapps
Hello everyone,

This is a quick tutorial on two functions known as include and require.

All that include and require do is grab all the text out of the file you've included/required and puts it into your document. The difference with include and require is that if there is an error with include i.e. can't find the file you've specified then your php code will just keep running. If there is an error with require your file will stop executing anymore code.

Two use include or require we need two files, the file we are including and our file we are calling include or require from. This example would connect to a database, we have our file that connects to the database and the file that stores our database details.

Our database details:
Code: Select all
<?php
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "password";
$dbDatabase = "someDatabase";
and here is the file we use to connect:
Code: Select all
<?php

require 'pathToFile/databaseDetails.php';

$connection= mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbDatabase);

//Check our connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
Include:
Code: Select all
<?php

include 'pathToFile/databaseDetails.php';

$connection= mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbDatabase);

//Check our connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
If we have a lot of files that connect to the database then this can be used to save time, one line instead of five, but also if you change your database details you only have to change one files. Of course also, you can use this for many other things.

I hope you all liked my quick tutorial and if you learnt anything give me a +rep :) thanks

#Birthday
Last edited by smashapps on Wed Jul 16, 2014 11:18 pm, edited 1 time in total.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: PHP: Include and Require
Dummy1912
maybe we are wrong but
where is the include part :)

#Birthday
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: PHP: Include and Require
smashapps
Oops,

It's the same syntax as require though, so just use:
Code: Select all
<?php

include 'somefile.php';

?>
#Birthday
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: PHP: Include and Require
Dummy1912
hi,
so what the different then between them?
what does require
and does include

#Birthday
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: PHP: Include and Require
smashapps
require and include both use text from a seperate file in your code but if require has an error the code will stop executing, if include has an error it will continue to execute but without the file you wanted to include.

#Birthday
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
5 posts Page 1 of 1
Return to “Tutorials”