开发者

what is the need of 'require' when all one ever needs is 'require_once' in php?

Hi I 开发者_如何学Pythonam quite new to php ,so pls forgive my naive question. Why does one ever need to use require when actually we need only require_once.It would be great if someone could just point me out to some examples where you need to use require and not require_once


You may want to include some code more than once.

Navigation, multiple times (top and bottom of the page). Analytics code, just the once.

Require is faster since require once has to check the file has not already been included.


you need require_once only if you can require it in differnt places and should avoid double require

Overwise, you should use require to stop any checks of require_once(it is some extra ms)


Basically, you use require_once/include_once for libraries and classes - you don't want them to be loaded many times (there will be "function/class already exists" errors). Require/include is widely used for including a HTML snippets, because sometimes you indeed need one snippet to be loaded (and displayed) a few times, like an advert or menu or even a part of table (which is generated by iterating and including over and over again same snippet).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜