This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
It used to be hard to find questions about operators and other syntax tokens.¹
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from …
这可能被认为是一个非常愚蠢的问题,但我正在为一个网站组建一个简单的模板系统,并试图跟踪我的变量使用情况并保持一切都干净整洁.
你能告诉我以下方法是否有任何优点/缺点:
简单的var:
$tpl_title = 'my title'
$tpl_desc = 'my text'
Run Code Online (Sandbox Code Playgroud)
数组:
$tpl['title'] = 'my title'
$tpl['desc'] = 'my text'
Run Code Online (Sandbox Code Playgroud)
宾语:
$tpl->title = 'my title'
$tpl->desc = 'my text'
Run Code Online (Sandbox Code Playgroud)
我喜欢对象方法最好,因为它看起来很干净,当html中的echo而不是数组和afaik它可以以类似数组的方式使用?但是,我想知道的是,以这种方式使用对象是否被视为不良做法或引入不必要的开销?