Jump to: navigation, search

Php require once

From w3cyberlearnings

Contents

require_once

require_once keyword uses to include a file and it is similar to require() function but require_once check whether the file has been included previously. If the file has already been included, it will not include it again.

Syntax

require_once "filepath/filename.php";
require_once "filepath/textfile.txt";

Note

This function produce a fatal E_COMPILE_ERROR level error when failed to include a file.

Example 1

mylist.txt

Leader leads our road path.<br/>
Mother cooks our dinner.<br/>
Father waste our cloths.<br/>
Mother and Father lead our path.<br/>
If leader leads our road path,<br/>
and Mother and Father is a leader.<br/>
Both of them lead our path.<br/>

test.php

<?php

require_once 'mylist.txt';
echo 'In the word';
require_once 'mylist.txt';

?>

Output

Leader leads our road path.
Mother cooks our dinner.
Father waste our cloths.
Mother and Father lead our path.
If leader leads our road path,
and Mother and Father is a leader.
Both of them lead our path.
In the word

Example 2

header.php

<?php

echo '<p>Welcome to my program!</p>';
echo '<hr width="100%"/>';
?>

footer.php

<?php
echo '<hr width="100%"/>';
echo 'copyright 2012';
?>

main.php

<?php

require_once "header.php";
echo "This is in main page<br/>";
require_once "header.php";
require_once "footer.php";
?>

Output

Welcome to my program!


This is in main page

copyright 2012

Related Links


Navigation
Web
SQL
MISC
References