亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 中 5 個條件的 If-else 語句

PHP 中 5 個條件的 If-else 語句

PHP
小唯快跑啊 2023-08-19 16:52:06
我有一個場景,我必須使用 if-else 語句匹配 5 個變量的所有可能條件,變量的值可以是 0 或 1,例如if($a === 0 && $b === 0 && $c === 0 && $d === 0 && $e === 0) {     echo 'case_1';} else if ($a === 1 && $b === 1 && $c === 1 && $d === 1 && $e === 1) {    echo 'case_2';} else if ($a === 0 && $b === 1 && $c === 1 && $d === 1 && $e === 1) {    echo 'case_3';} else if ($a === 0 && $b === 0 && $c === 1 && $d === 1 && $e === 1) {    echo 'case_4';} else if ($a === 0 && $b === 0 && $c === 0 && $d === 1 && $e === 1) {    echo 'case_5';} else if ($a === 0 && $b === 0 && $c === 0 && $d === 0 && $e === 1) {    echo 'case_6';} else if ($a === 1 && $b === 0 && $c === 0 && $d === 0 && $e === 0) {    echo 'case_7';} --------這樣的情況可能有幾十個,管理起來很復雜,所以我想知道是否有更好、更穩健的方法來實現這一點?更新:變量也可以是字母數字,$none = 'none'; // Default valueif($a === $none && $b === $none && $c === $none && $d === $none && $e === $none) {     echo 'case_1';} else if ($a === 1 && $b === 1 && $c === 1 && $d === 1 && $e === 1) {    echo 'case_2';} else if ($a === $none && $b === 1 && $c === 1 && $d === 1 && $e === 1) {    echo 'case_3';} else if ($a === $none && $b === $none && $c === 1 && $d === 1 && $e === 1) {    echo 'case_4';} else if ($a === $none && $b === $none && $c === $none && $d === 1 && $e === 1) {    echo 'case_5';} else if ($a === $none && $b === $none && $c === $none && $d === $none && $e === 1) {    echo 'case_6';} else if ($a === 1 && $b === $none && $c === $none && $d === $none && $e === $none) {    echo 'case_7';} ------------
查看完整描述

3 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

我正在嘗試想一個更靈活的方法,但現在;您可以將變量組合成一個字符串并檢查:


switch($a.$b.$c.$d.$e) {

    case '11111':

        echo 'case_1';

        break;

        

    case '00000':

        echo 'case_2';

        break;

        

    //etc...

}

至于您的編輯,只需在以下情況下使用變量:


    case $none.$none.$none.$none.$none:

    //or 

    case $none.'1111':

    //etc


查看完整回答
反對 回復 2023-08-19
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

目前,我只能告訴你匹配表達式的存在,但僅在 PHP8 中可用:

$case = match($a.$b.$c.$d.$e) {

? ? '00000' => 'case_0',

? ? '11111' => 'case_1',

? ? ...

? ? // default => something,

};

這只是一個“小”switch


查看完整回答
反對 回復 2023-08-19
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

好吧,在不理解的情況下,您的特定用例是什么,只是您想檢查什么本質上等同于某種編碼狀態......我冒昧地展示了一個完全演示的用例。


這個例子的核心是這個 parseCrumbs 函數,它的抽象是這樣的......



    function parseCrumbs($crumbs, $tranformer,  $text = '')  {


        // provide a list of methods, to carry out some sort of tranformation, based on positions

        static $places = ['negative', 'thousands', 'hundreds', 'tens', 'ones'];


        if(is_string($crumbs)) 

            $crumbs = str_split($crumbs, 1);


        // Reduce $crumns through transformations...

        foreach ( array_combine( $places, $crumbs ) as $method => $value ){

            if($value) $text .= " " . $tranformer::$method($value, $text);

        }


        return  $text === '' ? '-- empty --' : $text . PHP_EOL;


    }

模擬用例演示:將 5 位置狀態字符串轉換為表示單詞總數...



    class Transformations {


        static public function ones ($count, &$text ) {

            if($count)

                return ($text === '' || $text !== 'Minus ') ? "( {$count} ) Ones" : "and ( {$count} ) Ones";

            return '';

        }


        static public function tens ($count, &$text ) {

            if($count)

                return "( {$count} ) Tens";

            return '';

        }


        static public function hundreds ($count, &$text ) {

            if($count)

                return "( {$count} ) Hundreds";

            return '';

        }


        static public function thousands ($count, &$text ) {

            if($count)

                return "( {$count} ) Thousands";

            return '';

        }


        static public function negative ($count, &$text ) {

            if($count)

                return  'Minus';

            return '';

        }

    }


    function parseCrumbs($crumbs, $translations,  $text = '')  {

        static $places = ['negative', 'thousands', 'hundreds', 'tens', 'ones'];


        if(is_string($crumbs)) 

            $crumbs = str_split($crumbs, 1);


        foreach ( array_combine($places, $crumbs ) as $method => $value ){

            if($value) $text .= " " . $translations::$method($value, $text);

        }


        return  $text === '' ? '-- you need money --' : $text . PHP_EOL;


    }


    // These examples use a String to encode the state, 

    // but the parseCrumbs function can also accept an array...


    echo parseCrumbs('00001', Transformations::class); // ( 1 ) Ones

    echo parseCrumbs('00011', Transformations::class); // ( 1 ) Tens ( 1 ) Ones

    echo parseCrumbs('00111', Transformations::class); // ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones

    echo parseCrumbs('00111', Transformations::class); // ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones

    echo parseCrumbs('01111', Transformations::class); // ( 1 ) Thousands ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones

    echo parseCrumbs('10001', Transformations::class); // Minus ( 1 ) Ones

    echo parseCrumbs('10011', Transformations::class); // Minus ( 1 ) Tens ( 1 ) Ones

    echo parseCrumbs('10111', Transformations::class); // Minus ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones

    echo parseCrumbs('10111', Transformations::class); // Minus ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones

    echo parseCrumbs('11111', Transformations::class); // Minus ( 1 ) Thousands ( 1 ) Hundreds ( 1 ) Tens ( 1 ) Ones


為了演示排列...


    for( $i = 5000; $i > 0; $i--)

        echo parseCrumbs(str_pad($i, 5, "0", STR_PAD_LEFT), Transformations::class);



    // ( 5 ) Thousands

    // ( 4 ) Thousands ( 9 ) Hundreds ( 9 ) Tens and ( 9 ) Ones

    // ( 4 ) Thousands ( 9 ) Hundreds ( 9 ) Tens and ( 8 ) Ones

    // ... 4996 other permutations

    // ( 1 ) Ones


當按照上面的方式建模時,大約有 49 行代碼,具體取決于您的轉換...您擁有處理 5000 種狀態的邏輯...


顯然,您的用例需要自定義,但“邏輯”可能會保持簡單明了。


在查看了上面的評論后,我整理了一個示例,其中包含類似于復選框類型輸入的數組......


    class ShirtOptions {


        public function __construct(){

            $this->options = (object)[];

        }


        public function x_large ( $state ) {

            if ($state) $this->options->shirt_size = "x_large";

        }


        public function xx_large ( $state ) {

            if ($state) $this->options->shirt_size = "xx_large";

        }


        public function xxx_large ( $state ) {

            if ($state) $this->options->shirt_size = "xxx_large";

        }


        public function small ( $state ) {

            if ($state) $this->options->shirt_size = "small";

        }


        public function customPrinting ( $state ) {

            if($state){

                $this->options->custom_printing = true;

            } else {

                $this->options->custom_printing = false;

            }

        }


        public function templateID ( $state ) {

            if($state){

                $this->options->template_id = $state;

            }

        }


        public function parseOptions( $params ){

            static $options = [ "x_large", "xx_large", "xxx_large", "small", "customPrinting", "templateID" ];


            if(is_string($params))

                $params = str_split($params, 1);


            foreach ( array_combine( $options, $params ) as $option => $value )

                if($value) $this->$option($value);


            return $this;

        }


        public function to_json(){

            return json_encode($this->options, JSON_PRETTY_PRINT);

        }

    }


        


    echo (new ShirtOptions())->parseOptions([1,0,0,0,1,475])->to_json();

    {

        "shirt_size": "x_large",

        "custom_printing": true,

        "template_id": 475

    }


查看完整回答
反對 回復 2023-08-19
  • 3 回答
  • 0 關注
  • 212 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號