php - 空返回值
问题描述
我想做一个函数来获取交易代码,但返回值为空。
我从我的模型中调用这个函数。
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;
}
推荐阅读
- ios - 从 0 开始时将 Int 保存到 UserDefaults
- excel - 查找关键字并复制数据范围
- ruby-on-rails - 查询关联值
- angular - 可观察订阅两次
- mysql - 在 yii2 中运行原始 SQL 时出现 SQL 查询语法错误或访问冲突
- android - 如何使用“异步任务”获取存储在“房间”库中的所有行?
- c# - 从 Revit 加载项调用 Enity Framework 方法会导致 NotSupportedException
- php - Laravel 5.5 无法使用身份验证脚手架登录
- android - 当我想运行应用程序时,Android Studio 没有显示任何设备
- api - 如何通过 Ballerina 中的菜单驱动应用程序调用服务