duanbairan4235 2017-10-13 18:36
浏览 70

如何以不同的方式使用少数实体? 这些实体实际上叫什么?

I'm learning PHP.

While learning PHP, I noticed that there are few entities in PHP which are used in different fashion. Sometimes with the pair of parenthesis with the enclosing parameter/s between them, sometimes with the pair of blank parenthesis(i.e. without any parameter/s or message/s passed) and sometimes without using a pair of parenthesis.

How could this be possible? Is it possible for all other entities(i.e. functions / keywords / language constructs) also that are present in PHP?

Are there any other such entities present in PHP other than 'echo', 'print', 'die', 'exit' which I come across till now?

What these entities actually are called functions / keywords / language constructs in PHP?

What's the correct method / way / coding standard to access such type of entities?

It would be better for me as well as other PHP community members who are trying to learn PHP if someone could answer all of my queries in a simple, lucid and easy to understand language with the reliable explanation?

Thank You.

  • 写回答

1条回答 默认 最新

  • dongxun1142 2017-10-13 19:20
    关注

    Normally when you see () after something in PHP, whether there are parameter(s) inside it or not, it's a function.

    There are two cases where the parentheses are optional.

    1. Language constructs - the ones you've already mentioned (echo, print, die, and exit) as well as break, include, require, require_once, return, and yield. (I think that's all of the ones with optional parentheses.)
      Some language constructs, such as unset and array do require parentheses. If you're not sure whether a certain word is a function or a language construct, the best thing to do is consult the PHP manual. Language constructs will be included in this list of keywords. Some language constructs are also listed in the function reference, (for example, echo is included with string functions) but the specific manual pages will clearly indicate whether the term is a language construct, as well as whether or not parentheses are required.
      This answer has a detailed description of why/how language constructs can be used without parentheses.

    2. Object constructors with no required arguments.
      If an object constructor takes no arguments, or all of its arguments are optional, (for example function __construct($foo = null)) then the parentheses can be omitted when creating a new object. For example, for example, $now = new DateTime; and $now = new DateTime(); will both work.

    As far as the "correct" way, there really isn't one. If the parentheses are required, obviously the correct way is to use them, or your code won't parse. If the parentheses aren't required, it depends on the coding standard used in the project you're working on.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?