PHP: Include and Require
5 posts
Page 1 of 1
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:
I hope you all liked my quick tutorial and if you learnt anything give me a +rep
thanks
#Birthday
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
and here is the file we use to connect:<?php
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "password";
$dbDatabase = "someDatabase";
Code: Select all
Include:<?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();
}
Code: Select all
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. <?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();
}
I hope you all liked my quick tutorial and if you learnt anything give me a +rep

#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!
maybe we are wrong but
where is the include part
#Birthday
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
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
Oops,
It's the same syntax as require though, so just use:
It's the same syntax as require though, so just use:
Code: Select all
#Birthday<?php
include 'somefile.php';
?>
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
hi,
so what the different then between them?
what does require
and does include
#Birthday
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
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
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
#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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023