开发者

Is PHP's require_once() what I need if I want my site to load if AND ONLY IF the menu loads correctly?

开发者 https://www.devze.com 2022-12-24 07:58 出处:网络
I\'m kind of confused. I would put all the HTML fo开发者_如何学编程r the menu in a .php file and have the engine require it right?According to the manual, the require() function will produce a fatal

I'm kind of confused.

I would put all the HTML fo开发者_如何学编程r the menu in a .php file and have the engine require it right?


According to the manual, the require() function will produce a fatal error if it cannot find the required file. So, if you require() the menu and the menu file is not present, you page will not show.
But if the menu file is found and contains errors, the page will show them.


require_once(), and more specifically require() in general, will throw a fatal error (which will cause the page to stop loading) if the file does not exist. If there is an error in the file itself, require() will not change what happens. require_once() (and include_once()) are used in the case that you might have two or more files that need the same file to work. Basically, the _once functions will not let the same file be included multiple times (which is good, because you cannot redeclare functions and such).


Instead of using require() to load blocks of HTML, just use:

print file_get_contents('html/menu.html');


Dunno what do you call "loads correctly", but yes, you can put all the HTML for the menu in a .php file and have the engine require it


These are the four basic possibilities:

  • require_... stops the script if there's an error
  • include_... continues execution if there's an error
  • ..._once only loads the file the first time you use it
  • ..._nothing loads the file every time you use it

Of course, in normal circumstances, there should never be errors ;-)

0

精彩评论

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