PHP include_once statement

PHP include_once statement is very similar to PHP include statement but the only difference is include_once statement include PHP file only one time in any page. On the other hand, PHP include statement include a file many times.  

Structure: 
The structure of PHP include_statement is 
                        include_once("FileName") 
                            include_once("function.php")

Example:

Suppose that, we have three PHP file where first one has a function that print 0 to 10. Second PHP file include the first file and show the output. The third one add those two file. Now the code snippet are 
first.php
<?php
function foo()
{
    for($variable=0; $variable<10;$variable++)
    {
        echo $variable;
        echo "<br>";
        }
}
?>
Now the second file which one show the function value and second file name is  second.php
<?php
include("first_code.php");
foo();
?>
Third file is third.php
<?php
include("comments.php");
include("first_code.php");
?>
Try to run the third code. This code show an error message like this

Fatal error: Cannot redeclare foo()  previously declared  

This occurs because foo function declare in two times. So we use include_once statement to solve this problem.

<?php
include("comments.php");
include_once("first_code.php");
?>

If you have any problem make sure your comment we try our best to solve your problem. 

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Homebrew uninstall