dsnrixf6765 2016-09-12 17:24
浏览 83
已采纳

如果函数返回值,则有条件地赋值变量

I've been out of PHP programming for a while, but I'm almost sure I used to be able to write something like the following:

function checkData($data, $moreData) {
    if ($foo != validate($data)) {
        return false;
    }
    if ($bar != validate($moreData)) {
        return false;
    }
    $result = "$foo" . "$bar";
    return $result;
}

...where "$foo" and "$bar" haven't been set yet and where the "validate()" function either returns validated data, or returns false if validation fails.

I can't figure out where I'm going wrong, but that code (and variations of it) is throwing an "Undefined variable" error for $myVar.

What would be the correct syntax for this concept?

  • 写回答

2条回答 默认 最新

  • dongpengyu1363 2016-09-12 17:38
    关注

    I think you meant a little bit different thing. Instead of

    if ($foo != validate($data)) {
    

    You've been using this

    if (!$foo = validate($data)) {
    

    What is happening there is: 1. Call validate function 2. Assign the result to variable 3. Check if condition for that variable. It's kind of the same as

    $foo = validate($data)
    if(!$foo)
    

    But as you can understand it's not recommended way of doing the things as it's hard to read and needs explanation(otherwise why do u ask it here, hehe)

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

报告相同问题?

悬赏问题

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