如何解决空返回值
我想做一个获取交易代码但返回值为空的函数。
我从我的模型调用这个函数。
public function getTransactionCode($type){
switch($type)
{
case '52';
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "LAP-00{$next}";
return $code;
case '53';
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "PC-00{$next}";
return $code;
case '54';
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "TAB-00{$next}";
return $code;
case '55';
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "OOE-00{$next}";
return $code;
}
}
我想在我的控制器上使用这个函数从我的模型中提取任何值。
$code = $this->equipment->getTransactionCode($type = '');
但它一直返回空值。
解决方法
正如@shaedrich 所建议的,switch
不返回任何内容。但是,您可以为变量赋值,然后在 switch
语句后返回。
也应该在case
之后有冒号(:)而不是分号(;)
public function getTransactionCode($type){
$code = 'ANY DEFAULT VALUE'; // Needs to set any desired value here
switch($type)
{
case '52':
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "LAP-00{$next}";
break;
case '53':
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "PC-00{$next}";
break;
case '54':
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "TAB-00{$next}";
break;
case '55':
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
$code = "OOE-00{$next}";
break;
}
return $code;
}
,
switch
并不是真正为 return
设计的。
public function getTransactionCode($type){
$count = $this->where('type_id',$type)
->count('id');
$next = (intval($count) ?: 0) + 1;
if ($type === '52') {
return "LAP-00{$next}";
} else if ($type === '53') {
return "PC-00{$next}";
} else if ($type === '54') {
return "TAB-00{$next}";
} else if ($type === '55') {
return "OOE-00{$next}";
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。