require in php,為什么要在PHP中包含? - Why include when you can require in PHP? - 开发者知识库...

What is the point of attempting to include or include_once a library in PHP if you often have to use require or require_once instead (as they're usually critically important)?

如果您經常需要使用require或require_once,那么嘗試在PHP中包含或包含一個庫有什么意義呢(因為它們通常非常重要)?

9 个解决方案

#1

31

The difference between include/include_once and require/require_once is that when a file is not found, the former triggers a warning whereas the latter triggers an error. Opinions may vary but IMO, if a needed file is not present or readable for some reason, this represents a very broken situation and I would rather see an error and halt execution, than have a warning and proceed execution in a clearly broken context. So I believe it to be best practice to use require/require_once exclusively and avoid include/include_once altogether.

include/include_once和require/require_once之間的區別在於,當沒有找到文件時,前者觸發一個警告,而后者觸發一個錯誤。意見可能會有所不同,但是在我看來,如果由於某種原因需要的文件不存在或無法讀取,那么這就代表了一個非常糟糕的情況,我寧願看到一個錯誤並停止執行,也不願在一個明顯破損的上下文中發出警告並繼續執行。所以我認為最好的做法是只使用一次require/require_once,避免包含/include_once。

#2

45

Sometimes you are including files from libraries that you do not control or your code is being run on different environments.

有時,您會包含來自不受控制的庫的文件,或者您的代碼正在不同的環境中運行。

It is possible that some of these aren't actually required for your script but are helper files or extras.

有可能其中的一些並不是腳本所需要的,而是幫助文件或附加文件。

For instance, if your PHP is generating HTML, you might use an include to add a banner. But you wouldn't want your file to halt if for some reason that banner file was missing. You'd want to gracefully fail instead.

例如,如果您的PHP正在生成HTML,您可以使用include來添加橫幅。但是,如果由於某種原因丟失了banner文件,您不會希望文件停止。你會想要優雅地失敗。

#3

7

include - this attempts to load a file but does not halt execution if it fails

include—嘗試加載文件,但如果失敗,則不會停止執行

include_once - does the same as 'include' but runs an additional check to make sure the file hasn't already been included in the current script

include_once -與“include”相同,但運行額外的檢查以確保文件尚未包含在當前腳本中。

require - this attempts to load a file and throws a fatal error on failure

require -這將嘗試加載文件並在失敗時拋出致命錯誤

require_once - same as 'require' but runs an additional check to make sure it's loaded only once

require_once—與“require”相同,但是運行一個額外的檢查以確保它只被加載一次

If you don't need to use include_once or require_once try to avoid them since the extra check adds a little overhead.

如果您不需要使用include_once或require_once,請盡量避免它們,因為額外的檢查會增加一些開銷。

EDIT: I should add that they key difference is that require fails at the compiler level, while include throws an error at run-time.

編輯:我應該補充一點,它們的關鍵區別在於,在編譯器級別上需要失敗,而include在運行時拋出一個錯誤。

#4

3

Require will stop the script if it can't include the file.

Include will just give an error message.

如果腳本不能包含文件,則需要停止腳本。Include只會給出一個錯誤消息。

Btw.: *_once is slow. Shouldn't use it unless you really need to.

順便說一句。:* _once是緩慢的。除非你真的需要,否則不要使用它。

#5

2

Because you don't want the missing files stop your show. ;)

因為你不希望丟失的文件阻止你的表演。,)

#6

2

All four can take a local file or URL as input. require() and include() functions are virtually similar in their function except for the way they handle an irretrievable resource. include() and include_once() gives warning if the resource cannot be retrieved and try to continue execution of the program(if possible) while require() and require_once functions stops processing the page if they cannot find the resource.

這四個都可以使用一個本地文件或URL作為輸入。require()和include()函數在其函數中實際上是相似的,只是它們處理不可回收資源的方式不同。include()和include_once()在無法檢索資源時發出警告,並嘗試繼續執行程序(如果可能的話),而require()和require_once函數在無法找到資源時停止處理頁面。

It is best to use require_once() to include files which contains code and include_once() to include files that do not contains code e.g. HTML, CSS, etc. (This is my approach other's can differ.)

最好使用require_once()來包含包含包含代碼和include_once()的文件來包含不包含代碼的文件,例如HTML、CSS等等(這是我的方法,其他人的方法可能不同)。

閱讀:http://arin.me/blog/php-require-vs-include-vs-require_once-vs-include_once-performance-test

#7

2

Obviously, include will only generate a warning...

顯然,include只會生成一個警告……

So, if you have potentially missing files (such as dynamically including templates that may be safe to fail) and you are suppressing warnings (or don't mind bloated logs) then file_exists & require simply becomes include.

因此,如果您有可能丟失的文件(例如動態地包含可能安全失敗的模板),並且您正在抑制警告(或者不介意膨脹日志),那么file_exist & require將成為include。

However, that doesn't seem like a very common use case. The reason that include is still very common (Occam's Razor?) is more likely, simply this:

然而,這似乎不是一個非常常見的用例。其中包括的原因仍然很常見(奧卡姆剃刀?)

include makes more semantic sense, and is seen in other languages.

包含更多的語義,在其他語言中也可以看到。

#8

0

php http://tr.php.net/manual/en/function.include.php文件的開頭是:

The include() statement includes and evaluates the specified file.

include()語句包含並計算指定的文件。

The documentation below also applies to require().

下面的文檔也適用於require()。

以及http://tr.php.net/manual/en/function.require.php的文檔說明:

require() is identical to include() except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.

require()與include()是相同的,除了失敗之外,它還將產生一個致命的E_COMPILE_ERROR級別錯誤。換句話說,它將停止腳本,而include()只發出警告(E_WARNING),允許腳本繼續。

See the include() documentation for how this works.

請參閱include()文檔,了解這是如何工作的。

I think that's enough :)

我想這就足夠了

#9

0

Difference just in error handling, when required file doesn't exists: in case of include you can easily handle warning, change execution to show normal 'Sorry, error' page, send notification to site's admin.

In case of require your script will just stopped, fatal error is more difficult to handle, and you can't show any message to user.

不同之處在於錯誤處理,當需要的文件不存在時:如果包含您可以輕松處理警告,更改執行以顯示正常的“抱歉,錯誤”頁面,發送通知給站點的管理員。如果需要您的腳本將會停止,致命錯誤更難處理,並且您不能向用戶顯示任何消息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值