dongsanhu4784 2014-12-29 11:29
浏览 47

PHP从函数中获取数组值

I have the following function:

function telephoneNums($telephoneNum) {
    $telephoneNum = trim($telephoneNum);
    $telephoneNum = preg_replace("/[^0-9]/", '', $telephoneNum);

    if($telephoneNum !=8){
        $errorMsg[] = 'The contact number must be exactly 8 charators long';
    }


    return $telephoneNum;
    return array_values($errorMsg[]);
}

I am managing to return $telephoneNum but I am not managing to return $errorMsg[] - I am getting the following error PHP Fatal error: Cannot use [] for reading

I also tried return $errorMsg[]; but I still get the same errors.

How can I return $errorMsg[]

  • 写回答

5条回答 默认 最新

  • duanouyong4228 2014-12-29 11:31
    关注

    Just try like

    return array_values($errorMsg);
    

    And you also having return statement before this line.Try to return at once.In your case after the first return statement it won't return the second value.

    Also just initialize the $errorMsg like

    $errorMsg = array();
    

    If your IF condition not satisfies then at least it should be initialized with empty value or as an array.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向