Jump to: navigation, search

Php include once

From w3cyberlearnings

Contents

include_once

include_once keyword is used to include file content from another file, and it is similar to include(). When include_once include a file, it will check whether the file has already included. If the file has already included, it will not include the file again.

Syntax

include_once 'filepath/file';

Example 1

file1.php

<?php
    echo "Hello world. <br/>";
    echo "Good to hear. <br/>";
?>

file2.php

<?php
        include_once 'file1.php';
        include_once 'file1.php';
        echo "In this main file";
?>

Output: file1.php included only once

Hello world.
Good to hear.
In this main file 

Example 2

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/>

mypage.php

<?php

include_once 'mylist.txt';
echo 'In the world.';

include_once 'mylist.txt';
?>

Output: The content from mylist.txt is display only once

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 world.


Related Links


Navigation
Web
SQL
MISC
References