在php中用花括号将过程块包装起来有什么用? [英] What does wrapping a procedural block in curly braces do in php?

查看:104
本文介绍了在php中用花括号将过程块包装起来有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在/grp.php中遇到这个小问题时,我最近正在浏览Dolphin CMS的一些代码(PHP仇恨者的一个举足轻重的示例):

{
    switch ( $_REQUEST['action'] ) {
        //a crapton of switch statements
    }
}

我很好奇没有任何关键字的大括号"{}"是关于什么的.我主要从事PHP开发,所以(希望?!??)这是我不知道的事情,但是我尝试删除花括号,并使代码照常运行.

我很难理解wtf的目的.

有什么想法吗?

(注意:这实际上只是出于我自己的目的.我几乎无法入睡,因为这对我来说没有任何意义...)

解决方案

我已经在很多程序代码中看到了这一点.似乎可以将通常被抽象为单独功能的逻辑块组合在一起.这在某种程度上有助于编辑,并且您的IDE可以通过花括号快速识别出该块.

从功能上讲,我认为它什么也没做.从视觉上讲,它有助于对代码进行分组.

I was going through some of the code for Dolphin CMS recently (a craptastic example for PHP haters) when I ran across this little gem in /grp.php:

{
    switch ( $_REQUEST['action'] ) {
        //a crapton of switch statements
    }
}

I'm curious what the wrapping curly braces, "{}" without any keywords, are about. I do PHP development primarily so maybe (hopefully??!!?) this is something I just don't know about but I've tried removing the braces and the code runs as usual.

I'm having a hard time understanding wtf the purpose for this would be.

Any ideas?

(NOTE: This is really just for my own edification. I'm almost having trouble sleeping because this isn't making any sense to me...)

解决方案

I've seen this in large blocks of procedural code. It seems to help group logical blocks together that might normally be abstracted to a separate function. This, somewhat, helps when editing and your IDE can quickly identify the block by the braces.

Functionally, I don't think it does anything. Visually, it helps group code.

这篇关于在php中用花括号将过程块包装起来有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆