简体   繁体   English

PHP中简短的if-else语句

[英]Short if-else statements in PHP

I have a variable called $day_price that can hold one of two values - either " weekday_price " or " weekend_price ". 我有一个名为$ day_price的变量,它可以容纳两个值之一-“ weekday_price ”或“ weekend_price ”。 If I'm echoing $day_price however I would like the value of weekday_price to be echoed as Weekday Price and the value of weekend_price to be echoed as Weekend Price . 如果我要回显$ day_price,我希望将weekday_price的值回显为Weekday Price,而将Weekend_price的值回显为Weekend Price

How can I achieve this without writing a long if-else statement like this? 如何在不编写类似if的长语句的情况下实现这一目标?

if ($day_price=="weekday_price") { 
    echo "Weekday Price";
}
else if ($day_price=="weekend_price") { 
    echo "Weekend Price";
}

You can do this without conditions: 您可以无条件执行此操作:

$res = ucwords(str_replace('_', ' ', $day_price));

For more information see ucwords and str_replace in PHP Manual 有关更多信息,请参见PHP手册中的 ucwordsstr_replace

There isn't a direct way (this is a "problem" for all programming languages). 没有直接的方法(这是所有编程语言的“问题”)。

You can, for example, build an array with key value pairs where key is weekend_price and value is Weekend Price 例如,您可以使用键值对构建一个数组,其中键为Weekend_price,值为Weekend Price

$tmpArray = array(
    "weekend_price" => "Weekend Price",
    "weekday_price" => "Weekday Price"
);
echo $tmpArray[$day_price];

Otherwise if you are sure you'll use "_" instead of spaces, and you want uppercase all words, you can use this: 否则,如果您确定将使用“ _”而不是空格,并且希望所有单词都大写,则可以使用以下命令:

echo ucwords(str_replace("_", " ", $day_price));

Another solution (which I don't like but can be used): 另一个解决方案(我不喜欢但可以使用):

function ConvertWeekPrices($day_price)
{
    $result = null;
    switch($day_price)
    {
        case "weekend_price":
            $result = "Weekend Price";
            break;
        case "weekday_price":
            $result = "Weekday Price";
            break;
    }
    // Do something with result if you want
    // Do some checks if result is null
    return $result;
}

Then in your code: 然后在您的代码中:

echo ConvertWeekPrices($day_price);

There are other approaches, you can use a database table, an xml file, there are unlimited combinations. 还有其他方法,可以使用一个数据库表,一个xml文件,有无限的组合。 I think those 2 are the easier approaches 我认为那2是更简单的方法

A literal answer would be 字面上的答案是

$trans = array (
  "weekday_price" => "Weekday Price",
  "weekend_price" => "Weekend Price",
);
echo $trans[$day_price];

however, there is a way to avoid such a translation, I believe. 但是,我认为,有一种避免这种翻译的方法。

echo ucwords( str_replace( '_', ' ', $date_check ) );

在PHP手册中查看ucwords

If you want to use it in different places, you could define an associative array: 如果要在其他地方使用它,则可以定义一个关联数组:

$texts = array('weekday_price' => 'Weekday Price',
               'weekend_price' => 'Weekend Price');

And use that like: 并像这样使用:

echo $texts[$day_price];

If it's only ever going to be weekend or weekday you could do: 如果只是周末或工作日,您可以:

echo ($day_price=="weekday_price") ? 'Weekday Price' : 'Weekend Price';

But i'd be careful when exchanging readability (therefore maintainability) for a few saved lines of code. 但是在为几行已保存的代码交换可读性(因此可维护性)时,我会小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM