includevsincludeonce

Include() vs Include_once() In PHP – Difference

Today we are going to learn the most basic thing in PHP which will be very helpful to beginners while exploring the PHP Language’s basic concepts. This concept is very small, however, you have to understand it properly if you want to be a good developer in PHP. So now let see what is the actual use of include() and include_once().

Syntax :

include('filename');
 
or
 
include_once('filename');

Functional Work :

Both functions have similar work. Both functions will include and evaluates the specific file while executing the code.

Functional Difference :

There is only one difference between these two functions. If the code a from a file has been already included then it will not be included again if we use include_once(). Means include_once() include the file only once at a time.

Let’s see one example for the same so we will get a better idea on this.

Assume that we have three files.

  • Helper.php
  • General.php
  • Index.php

These three files  have a code like below :

Helper.php

<?php 
Function displayMessage()
{
	echo  "This is Helper File";
}
?>

General.php

<?php 
include("Helper.php");
?>

Index.php

<?php 
include("General.php");
include("Helper.php"); 
?>

Now execute the Index.php file. You will get this error as a result.

Include

Fatal Error :

Cannot redeclare displayMessage() function. It’s already declared in Helper.php file. It will break the execution of your code because here we are getting a fatal error as a result. This is going to be happening because we have included the Helper.php file twice. So It will try to create the function two times with the same name. As per the PHP Coding standards, it will not allow you to create the same name function two times.

So If you  want to avoid such problems while programming then you must know the actual use of each function. Now we will see how to tackle such problems.

Now Please write the below code in Index.php file.

<?php 
include("General.php"); 
include_once("Helper.php");
displayMessage();
echo "<br/>This is Index File";
?>

Now Execute the Index.php file. You will not get any error. You will get an output like below.

Output :

This is Helper File

This is Index File

Benefit of using include() in place of require() :

These two perform the same function, however, the major difference is require() will throw a fatal error (E_COMPILE_ERROR )  since it is not able to find the specific file on specified location and it will break the execution as a result, while include() will show the warning (E_WARNING), however it will not break the script execution. To understand this in detail Click here…

17 thoughts on “Include() vs Include_once() In PHP – Difference

    1. Somebody necessarily lend a hand to make critically posts. This is the first time I frequented your web page and so far? I surprised with the research you made to make this particular put up extraordinary. Wonderful job!

  1. It’s a pity you don’t possess a donate button! I’d most certainly donate for this excellent blog!
    I guess for the time being i’ll be happy with bookmarking and adding your Feed to my Google account.
    I anticipate new updates and may discuss this website with
    my Facebook group. Talk soon!

  2. Hello all, here every person is sharing these kinds of familiarity,
    therefore it’s good to read this webpage, and I used to pay a visit this web site every day.

  3. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something informative to read?

  4. At this time it looks like Expression Engine is the best blogging platform out
    there right now. (from what I’ve read) Is that what you are using on your blog?

  5. Thanks for every other informative website.

    Where else could I get that kind of info written in such a perfect manner?
    I have a challenge that I’m simply now running on, and I’ve
    been at the glance out for such information.

  6. I do not have any idea how I finished up here, but I
    thought this post was good. I don’t know your identiity but definitely you’re attending a famous blogger when you aren’t already ;
    ) Cheers!

  7. I am getting this error
    Notice: Use of undefined constant product_id – assumed ‘product_id’ in /Applications/XAMPP/xamppfiles/htdocs/fashion/public/importcsv.php on line 49

    Modified following code:

    if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {
    $message .=”File uplaoded successfully.”;

    if (($getdata = fopen($target_file, “r”)) !== FALSE) {
    fgetcsv($getdata);
    while (($data = fgetcsv($getdata)) !== FALSE) {
    $fieldCount = count($data);
    for ($c=0; $c < $fieldCount; $c++) {
    $columnData[$c] = $data[$c];
    }
    $prod_serial_no = mysqli_real_escape_string($con ,$columnData[0]);
    $product_id = mysqli_real_escape_string($con ,$columnData[1]);
    $product_name = mysqli_real_escape_string($con ,$columnData[2]);
    $description = mysqli_real_escape_string($con ,$columnData[3]);
    $img_url = mysqli_real_escape_string($con ,$columnData[4]);
    $price = mysqli_real_escape_string($con ,$columnData[5]);
    $product_url = mysqli_real_escape_string($con ,$columnData[6]);
    $aff_company = mysqli_real_escape_string($con ,$columnData[7]);
    $brand = mysqli_real_escape_string($con ,$columnData[8]);
    $stock = mysqli_real_escape_string($con ,$columnData[9]);
    $size = mysqli_real_escape_string($con ,$columnData[10]);
    $size_value = mysqli_real_escape_string($con ,$columnData[11]);
    $color = mysqli_real_escape_string($con ,$columnData[12]);
    $color_value = mysqli_real_escape_string($con ,$columnData[13]);
    $main_cat = mysqli_real_escape_string($con ,$columnData[14]);
    $sub_main_cat = mysqli_real_escape_string($con ,$columnData[15]);
    $sub_cat = mysqli_real_escape_string($con ,$columnData[16]);
    $import_data[]="('".$prod_serial_no."','".product_id."','".$product_name."','".$description."','".$img_url."','".$price."','".$product_url."','".$aff_company."','".$brand."','".$stock."','".$size."','".$size_value."','".$color."','".$color_value."','".$main_cat."','".$sub_main_cat."','".$sub_cat."')";

  8. Pingback: Hemal Thakor

Leave a Reply