dongwei5794 2014-03-23 11:15
浏览 55

为什么php有两个不同的运算符添加?

I've came from C# and found annoying about php operators . and + what is the reason to have two different operators for the same purpose, addition?

So if there is a reason why php has these two, so why C# doesn't?

Sorry, for probably kind a dumb question, but for me it looks redundant and annoying.

  • 写回答

1条回答 默认 最新

  • douhuan2101 2014-03-23 12:31
    关注

    PHP has 'dynamic' or 'type not known until runtime' variables. i.e. the interpreter and/or the programmer cannot know until run time what type of information is in a variable. Now, let us assume that you have two variables and both contain character strings that can also be interpreted as numbers.

    i.e. $foo = "2014" and $bar = "0301";

    Now assume that the + plus sign has to work out what you want to do?

    Do you want these to be treated as numbers and added together? giving 2315.

    Or do you the 'sortable' date field of "20140301' i.e. yyyymmdd?

    There is no way for the '+' operator to know and do the correct thing in all circumstances.

    Hence we need the 'concatenation' operator '.'.

    The '+' operator always converts to numbers and adds.

    The '.' operator always converts to string and concatenates.

    We need both.

    In a language with 'typed' variables you can always work out from the context i.e. the types of the variables, what the programmer intended.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据