+ 2

Include in PHP

Are there any advantages using 'include' instead of 'require'? As I see it, one should always use 'require' since it will produce a fatal error and stop the script if the file is not found.

19th Sep 2018, 11:24 AM
Thomas Blok
Thomas Blok - avatar
2 ответов
+ 16
The only advantage of include is that it does not break the whole page if an included file isn't found.
19th Sep 2018, 2:26 PM
Igor Makarsky
Igor Makarsky - avatar
+ 1
<?php try { $path = "Idon'tKnowIfItExists.php"; if ( ! file_exists($path)) { throw new Exception('File does not exist'); } include $path; } catch (Exception $e) { header("Location: /?page=404"); } ?> https://code.sololearn.com/wGJO5GANO3hA/?ref=app
21st Sep 2018, 3:22 PM
Dmitriy
Dmitriy - avatar