PHP Intermediate

Intermediate PHP Tutorials

Our intermediate PHP tutorials have been produced for people who have some knowledge of PHP but need to develop that knowledge further. If this is too advanced for you then you should look at the basic tutorials or if this isn't advanced enough for you please look at our advanced tutorials.


Including and requiring files in other files

PHP often includes files in other files. Therefore this important article shows you how!

Including and requiring files in other files

Often you want to create PHP codes in seperate files for different tasks. For instance you might have one file database.php for accessing the database another file users.php for handling users and finally the main web page index.php.
In PHP this files can be included in the main page using include or require functions.
Both of these functions are interchangable in the below examples, with one difference explained below. The functions include the files so that when PHP evaluates the file with the includes it appears to be one file, therefore the functions are all visible to one another, excluding any object orientation issues.
database.php file
<?php
function connect()
{
 //connects to the database
}
?>

users.php file
<?php
function isUser($username)
{
 //checks if a username is an existing user.
}
?>

index.php file
<?php
//include the database file
include('database.php');
//include the users file
include('users.php');

function login($username, $password)
{
 //This function was present in the database.php file
 connect();
 //This function was present in the database.php file
 if (isUser($username))
 {
  //Carry on logging in
 }
}
?>
As you can see above the include functions have included the files in the index.php file allowing the index.php file to use the database.php and users.php functions. Of course any item be it variables, classes or functions etc can be used from the database.php and users.php functions.
require('user2.php');
Here the user2.php file doesn't exist, but because the require function is used it will throw a fatal error as the file requires function indicates that this file needs user2.php file to exectute. However if a include was used it would only return a warning as the file isn't required to execute. Another problem that can occur is that if a file is included in a child file and is included again in a parent file it can cause an error so below we have an example of this occuring.
database.php file
<?php
 /*
  * The rest of the database file
 /*
?>

users.php file
<?php
//include the database file
include('database.php');
 /*
  * The rest of the users file
 /*
 ?>

index.php file
<?php
//include the users file
include('users.php');
//Its included again!!
include('database.php');

/*
 * The rest of the index.php file
 */
?>
PHP will throw an error because the database.php file has been included twice, once in the users.php file and once in the index.php file.

Ensure a file is only included or required once

To ensure a file is only included or required once you need to use the include_once or require_once these files will only include a file once no matter how many times the file is included in the children files. This should always be used if there is a chance a file could be included multiple times. So for example...
database.php file
<?php
 /*
  * The rest of the database file
 /*
?>

users.php file
<?php
//include the database file
include_once('database.php');
 /*
  * The rest of the users file
 /*
 ?>

index.php file
<?php
//include the users file
include_once('users.php');
//Its included again!!
include_once('database.php');

/*
 * The rest of the index.php file
 */
?>
When the index.php file is evaluated it will not throw an error because the database.php file was only included once in the users.php file and the users.php was the only file included once in the index.php. As a rule it is usually always a better idea to use the include_once and require_once functions rather than than the include and require functions.

URL Querystrings

In web applications data can be sent through query strings in the web URL. This article explains how to create and read the query string data.

URL Querystrings

In PHP the querystring in the URL can be read with the global $_GET variable. The $_GET variable is an associative array where each querystring parameter is represented by a name and value. So the url www.example.com/index.html?page=1&id=5 can be read like so..
echo $_GET["page"];
echo $_GET["id"];
This outputs 1 and 5 for page and id respectivley. When creating a URL with querystring it is important to encode the variables as you can introduce characters into a URL that are illegal and therefore will not allow the URL to work. In PHP this is quite simple to do by using the urlencode function as below...
$url = sprintf("http://www.example.com/index.html?page=%s&title=%s", urlencode($page), urlencode($page));
Because you are encoding the parameters you will need to decode the parameters once you recieve them with the urldecode function. In fact this is good general practice whenever you read a querystring, so...
echo urldecode($_GET["page"]);
echo urldecode($_GET["id"]);
Decoding the querystring parameters ensures any characters that have been escaped are unescaped.

No comments:

Post a Comment