PHP: Include and Require
Posted: Tue Jul 15, 2014 12:31 pm
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 thanks
#Birthday