dongyanghan0556 2014-04-05 19:10
浏览 26
已采纳

PHP:Function - 返回2个变量(Boolean + $ variable)

Issue Resolved : Here is the solution :

function testCoreq()
    {
        $coreqTest = makeCoreq();

        if(empty($coreqTest))
        {
            return array(true);
            break;
        }
        else
        {
            foreach ($coreqTest as $ctest)
            {
                if($ctest['value'] == "true")
                {
                    return array(true);
                    break;
                }
                else
                {
                    return array(false,$ctest['coreqID']);
                }
            }
        }

    }

if(testCoreq()[0])
{
  //do something
}
else
{
  return testCoreq()[1]
}   

I'm doing a school project and hit kind of a bump.

I created a function and i want it to either return "true" (boolean) or "false" (boolean) + a variable.

I searched the net quite a bit but wan't able to find a simple way to do this .

Is there any way to this this ? //Thanks

The function is working properly but when it is returning the variable - it is also assuming that the function is returning "true" when i want it to return false + the value like :

else
                {
                    return $ctest['coreqID'];
                    return false;
                }

Here is the code :

function testCoreq()
    {
        $coreqTest = makeCoreq();

        if(empty($coreqTest))
        {
            return true;
            break;
        }
        else
        {
            foreach ($coreqTest as $ctest)
            {
                if($ctest['value'] == "true")
                {
                    return true;
                    break;
                }
                else
                {
                    return $ctest['coreqID'];
                }
            }
        }

    }

I am using it like this:

if (testCoreq())
{
 // do something
}
else
{
 // return the variable
}

but even if the first statement is false , then it is returning the variable - it is assuming the function is true.

  • 写回答

1条回答 默认 最新

  • dongzhong5833 2014-04-05 19:13
    关注

    You can try to return -1 on true and other for false to decrease amount of returning values. Next option is to return array of values. The other option would be to pass reference variable in the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法