

下記コードをcase文を使って書きたいです。

[code]

    return config('apps.flags.employee_generations.変数.name');
    
[/code]

みたいな感じにして書けるのかなと予想しているのですが、うまくできません。  
ご教授ください。

[code]

    $age = $this->employee_age;
    if ($age >= 20 && $age < 30) {
        return config('apps.flags.employee_generations.1.name');
    } elseif ($age >= 30 && $age < 40) {
        return config('apps.flags.employee_generations.2.name');
    } elseif ($age >= 40 && $age < 50) {
        return config('apps.flags.employee_generations.3.name');
    } elseif ($age >= 50 && $age < 60) {
        return config('apps.flags.employee_generations.4.name');
    } elseif ($age >= 60 && $age < 70) {
        return config('apps.flags.employee_generations.5.name');
    } elseif ($age >= 70) {
        return config('apps.flags.employee_generations.6.name');
    } else {
        return "";
    }
    
[/code]


CC BY-SA 4.0
2023-08-04T14:38:30.877
95847
2023-08-06T04:21:22.287
2023-08-04T16:04:55.950
3060
59369
question
php laravel
php case文で書き換え


[PHP: switch](https://www.php.net/manual/ja/control-structures.switch.php)

> もっと複雑な比較を行うには、`true` を `switch`文の値として指定するやり方が使えます。
[code]

    $age = $this->employee_age;
    switch (true) {
        case $age >= 20 && $age < 30:
            return config('apps.flags.employee_generations.1.name');
        case $age >= 30 && $age < 40:
            return config('apps.flags.employee_generations.2.name');
        case $age >= 40 && $age < 50:
            return config('apps.flags.employee_generations.3.name');
        case $age >= 50 && $age < 60:
            return config('apps.flags.employee_generations.4.name');
        case $age >= 60 && $age < 70:
            return config('apps.flags.employee_generations.5.name');
        case $age >= 70:
            return config('apps.flags.employee_generations.6.name');
        default:
            return "";
    }
    
[/code]


CC BY-SA 4.0
2023-08-04T15:30:01.607
95848
2023-08-04T15:30:01.607
47127
95847
answer

[code]

    <?php
    
        class Sample{
    
            public $employee_age ;
    
            function config($param){ return $param ; }
    
            function Check(){
                $age = $this->employee_age;
    
                switch(true){
                    case $age >= 70 : $ch = 6       ; break ;
                    case $age >= 60 : $ch = 5       ; break ;
                    case $age >= 50 : $ch = 4       ; break ;
                    case $age >= 40 : $ch = 3       ; break ;
                    case $age >= 30 : $ch = 2       ; break ;
                    case $age >= 20 : $ch = 1       ; break ;
                    default         : $ch = FALSE   ; break ;
                }
    
                return ($ch === FALSE) ? "" : $this->config("apps.flags.employee_generations.{$ch}.name");
            }
        }
    
        if($sample = new Sample()){
            for($age=19;$age<=71;$age++){
                $sample->employee_age = $age ;
                printf("%s,%s\n",$age,$sample->Check());
            }
        }
    
[/code]


CC BY-SA 4.0
2023-08-05T07:44:42.437
95854
2023-08-06T04:21:22.287
2023-08-06T04:21:22.287
59393
59393
95847
answer