Php-mysql Course

Once you have created a script or an HTML content that you want to use in multiple pages is best stored in an external file and included in each page you want to use it. This is accomplished with the include and require functions.
PHP has four commands that can be used to include code from an external file:

To include an external file, you pass the file path to one of these four include functions as a string (single or double quotes).
  - Syntax:
include('external_file.php');
    OR
require('external_file.php');

The file path can be either absolute or relative to the current document.
  - An absolute path says where a file is starting from the root directory of the computer.
<?php
include('C:/xampp/htdocs/includes/file.php');            // windows
include('/usr/MAMP/htdocs/includes/file.php');           // unix (linux)
?>

  - A relative path uses the position of the current script file as the starting point. To move back one folder, use two periods together followed by a slash (../).
<?php
include('file.php');                   // "file.php" is in the curent script directory
include('../includes/file.php');       // "file.php" is in the "includes" folder, which is in the back directory
?>

For example, if you have a standard header and menu files (in a "templ" directory located in the curent folder), called "header.php" and "menu.php", that should be used on all pages. To include these files in a PHP script, use the include() (or require() ) function:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
 <title>Title page</title>
<body>

<?php include('templ/header.php'); ?>

<div id="menu">
<?php include('templ/menu.php'); ?>
</div>

<div id="content">some content</div>

</body>
</html>
With this method you can insert the same header and menu code in any PHP files in the Web site, all pages in the Web site can include the code of the header and menu files, and if you need to update the header or the menu later, you will only need to make changes in the "header.php" or "menu.php" file.

Using parentheses with the include commands is optional, the following would also work:
                include 'includes/file.php';
                include '/usr/MAMP/htdocs/includes/file.php';

When using a relative file path, you can use use "./" to indicate that the path begins in the current folder. Example:
                include('./includes/file.php');

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
Include and Require

Last accessed pages

  1. Using v-model in form input fields (1051)
  2. jQuery UI draggable - Drag elements (11445)
  3. Display data from PHP Array, or MySQL in HTML table (26980)
  4. Redirects (4978)
  5. jsSHA - SHA Hashes and HMAC in JavaScript (3519)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (310)
  2. The Mastery of Love (48)
  3. CSS cursor property - Custom Cursors (36)
  4. Read Excel file data in PHP - PhpExcelReader (35)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (31)