Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数返回值数组和布尔值那么这个函数在php中的返回类型是什么?_Php - Fatal编程技术网

函数返回值数组和布尔值那么这个函数在php中的返回类型是什么?

函数返回值数组和布尔值那么这个函数在php中的返回类型是什么?,php,Php,我有一个如下的函数。它返回数组值和布尔值,所以我想知道如何定义函数返回类型?此函数的返回类型是什么 public function alert($id): ____ //this place value? { $model = Model::find($id); if ($model) { if ($model->status != 1) { return array(

我有一个如下的函数。它返回数组值和布尔值,所以我想知道如何定义函数返回类型?此函数的返回类型是什么

public function alert($id): ____ //this place value?
{
            $model = Model::find($id);
            if ($model) {
                if ($model->status != 1) {
                    return array(
                        'header' => 'Failed',
                        'message' => 'Failed related message'
                    );
                } else {
                    return array(
                        'header' => 'Success',
                        'message' => 'Success related message.'
                    );
                }
            }
            return false;
}

您可以使用此方法定义返回值:

public function alert($id): ? array
{
            $model = Model::find($id);
            if ($model) {
                if ($model->status != 1) {
                    return array(
                        'header' => 'Failed',
                        'message' => 'Failed related message'
                    );
                } else {
                    return array(
                        'header' => 'Success',
                        'message' => 'Success related message.'
                    );
                }
            }
            return null;
}

这将导致null或array。

php中内置了获取变量类型的功能-
gettype()
。还有
settype()
-感谢@mech的回复。你的回答与我的问题有关,我听不懂。我的问题是没有定义任何东西,它工作得很好。但我想知道如何为这种类型的条件定义返回类型?返回false?我不认为这是任何原因,您可以在函数定义@return属性中定义mixed,并在示例中注释它将返回null或array。