Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include, require & require_once

Today I've tried to include file that returns object. I always use require_once, however now I've noticed weird behavior of it.

File main.php

$lang = false;
$lang->name = "eng";
$lang->author = "Misiur";
$lang->text = "Text is test";
$lang->undefined = "Undefined";
return $lang;

File index.php

$lang = include('langs/eng/main.php');
var_dump($lang);
echo "<br />";
$lang = require('langs/eng/main.php');
var_dump($lang);
echo "<br />";
$lang = require_once('langs/eng/main.php');
var_dump($lang);

Result

object(stdClass)#9 (4) { ["name"]=>  string(3) "eng" ["author"]=>  string(6) "Misiur" ["text"]=>  string(12) "Text is test" ["undefined"]=>  string(9) "Undefined" }
object(stdClass)#10 (4) { ["name"]=> string(3) "eng" ["author"]=> string(6) "Misiur" ["text"]=> string(12) "Text is test" ["undefined"]=> string(9) "Undefined" }
bool(true) 

Why is it like that? I thought that require and require_once are same thing, only require_once is more safe because it won't duplicate include.

Thanks.

Edit:

But when i use only require_once, I get bool(true) too. So require_once returns only result of include, not it's content?

Edit2:

LOL. I haven't noticed, that earlier I had required this file inside of my class which is created before this code execution ($this->file = require_once("langs/$name/main.php");)

So require_once worked as it should. Thanks guys!

like image 447
Misiur Avatar asked Jun 04 '10 14:06

Misiur


People also ask

What is difference between include and require?

Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.

What are include () and require () functions?

These functions are the same if but they have one difference. The difference is that the include() function produces a warning, but the script will continue execution, while the require() function produces a warning and a fatal error i.e. the script will not continue execution.

What is the difference between require once and include?

They are all ways of including files. Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue.

What is the difference between include () and require () in PHP?

The require() function will stop the execution of the script when an error occurs. The include() function does not give a fatal error. The include() function is mostly used when the file is not required and the application should continue to execute its process when the file is not found.


2 Answers

When you use include and require you get the result of the page you're referencing being included. When you use require_once it checks and sees that the page has already been loaded using require, so it returns true to tell you that it has been loaded successfully at some point.

like image 114
tloach Avatar answered Oct 05 '22 18:10

tloach


That's exactly it, and you're trying to duplicate include. When you require_once something that hasn't been included or required before, you will get the return value.

EDIT: With this simple test on PHP 5.3.2, I get the return value when using require_once for the first time.

parent.php:

<?php
$first = require_once("child.php");
var_dump($first);
$second = require_once("child.php");
var_dump($second);
?>

child.php:

<?php
return "foo";
?>

It prints:

string(3) "foo"
bool(true)
like image 25
Matthew Flaschen Avatar answered Oct 05 '22 16:10

Matthew Flaschen