简体   繁体   English

什么是使用正确的方法包括或不带括号在PHP

[英]What is the proper way to use include with or without brackets in php

I already know how to use include , require and even require_once . 我已经知道如何使用includerequire甚至require_once What I have always practiced is this: 我一直练习的是:

for example include 'sample.php'; 例如, include 'sample.php'; or require_once 'classes/DB.php'; require_once 'classes/DB.php';

But in some ways I often see in some forums and tutorials and even here that they are using it like this - include ('sample.php'); 但是在某些情况下,我经常在一些论坛和教程中看到它们,甚至在这里,他们也像这样使用它include ('sample.php'); and require_once ('classes/DB.php'); require_once ('classes/DB.php'); .

I know that any of this will work, but I just want to know what would you recommend maybe as a good practice? 我知道任何一种方法都行得通,但是我只想知道您会推荐什么作为一种好的做法? XD and if it's already been asked here please show me the link because I can't find it. XD,如果在这里已经被询问过,请给我显示链接,因为我找不到它。

The include 'sample.php'; include 'sample.php'; or require_once 'classes/DB.php'; require_once 'classes/DB.php'; is a preferred way. 是首选方式。

Is preferred, it will prevent your peers from giving you a hard time and a trivial conversation about what require really is. 首选,这将防止您的同伴给您带来麻烦和琐碎的话题,这些话题实际上是什么。

Reference . 参考

Side Notes: 注意事项:

1. require/include not a function they are language construct same as echo . 1. require/include不包含功能它们是与echo相同的语言构造。 Credits: @Rahil comment. 鸣谢: @Rahil评论。

2. Also it will save time of pressing two keystroke ( & ) for a lazy developers like us :p 2.同样,对于像我们这样的懒惰的开发人员,也可以节省按下两个按键()时间:

include and the rest are not functions , they do not need the parentheses. include和其余的不是函数 ,它们不需要括号。 Parentheses are also used for grouping, for example 1 + (2 * 3) . 括号也用于分组,例如1 + (2 * 3) You can basically add as many parentheses around any expression as you want; 基本上,您可以根据需要在任意表达式周围添加尽可能多的括号; 1 + (2 * 3) is equivalent to (1 + (2 * 3)) is equivalent to 1 + ((2 * 3)) is equivalent to ((1) + (((2) * (3)))) . 1 + (2 * 3)等效于(1 + (2 * 3))等效于1 + ((2 * 3))等效于((1) + (((2) * (3))))

So all include ('file.php') is doing is add unnecessary grouping parentheses around the expression 'file.php' . 因此,所有include ('file.php')所做的就是在表达式'file.php'周围添加不​​必要的分组括号。 You may as well write include (((('file.php')))) , it has exactly the same effect, which is none. 您也可以编写include (((('file.php')))) ,它具有完全相同的效果,没有。 It's usually only done by people who do not understand this fact and believe the parentheses to be necessary "like with other functions", or maybe who like the style. 通常仅由不了解这一事实并认为括号是“像其他功能一样”或可能喜欢样式的人才能完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM