dongzhui9936 2014-07-29 13:37
浏览 31
已采纳

在PHP中,在传递对函数的引用时声明其变量类型是可选的吗?

im new to PHP and i have come across two ways that people declare their functions parameters.

function fun(array &$array)
{
   return $stuff
}

OR

function fun($array)
{
   return $stuff
}

From what i read in the documentation the & passes the reference to the variable. but i thought that declaring the type array was unnecessary.

So, when i pass reference to a variable in a function is declaring its variable type optional?

Meaning is function fun(array &$array) AND function fun(&$array) equivalent/allowed?

  • 写回答

2条回答 默认 最新

  • dqiz20794 2014-07-29 13:43
    关注

    To answer the question, Its optional because not all values are allowed. You can specify non scalar values (arrays, objects etc) but not ints strings floats etc

    Your last line they are the same, with the exception that a catchable fatal error is thrown if its called like this

    fun("1")
    fun(false)
    

    the way without the type hint would allow the function to be run and the functions logic would have to test that its argument is correct

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答