我可以使用具有多个条件的if语句吗? PHP

时间:2017-03-29 07:15:46

标签: php if-statement multiple-conditions

我打赌我可以,但它会像这样工作吗?

func setupCollectionView() {

    let layout = UICollectionViewFlowLayout()
    collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, testView.frame.size.width, height: testView.frame.size.height), collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.green
    testView.addSubview(collectionView)
    testView.clipsToBounds=true

    }

所以,如果function dutchDateNames($) { $day = explode('-', $date)[2]; $dutchday = ($day < 10) ? substr($day, 1) : $day; $month = explode('-', $date)[1]; if ($month == '01' . '02') { $dutchmonth = 'Januari' . 'Februari'; } $dutchdate = $dutchday . ' ' . $dutchmonth . ' ' . explode('-', $date)[0]; return $dutchdate } 是01,那么$ dutchmonth应该是Januari。如果$ month是02,$ dutchmonth应该是Februari,依此类推。 我觉得我没有这样做吗?

5 个答案:

答案 0 :(得分:1)

就像你不会在任何月份返回因为你连接(mounth 0102不存在)。

如果我正确理解你的问题,我认为阵列会更好:

$month = explode('-', $date)[1]; //Ok you use this data like an index

$letterMonth = ['01' => 'Januari', '02' => 'Februari', ....]; // Create an array with correspondance number -> letter month

$dutchmonth = $letterMonth[$month]; Get the good month using your index

答案 1 :(得分:0)

试试这个:

使用elseif条件

if ($month == '01') {
    $dutchmonth = 'Januari';
} elseif ($month == '02') {
    $dutchmonth = 'Februari';
} elseif ($month == '03') {
   $dutchmonth = '...'; 
} 

答案 2 :(得分:0)

创建查找数组并按键获取值:

$month = '02';
$months = [
    '01' => 'Januari'
    '02' => 'Februari'
    // more months here
];
$dutchmonth = isset($months[$month])? $months[$month] : '';
echo $dutchmonth;

答案 3 :(得分:0)

我认为正确的方法是将地图保存为数组。的 Demo

<?php
$array['01'] = 'Januari';
$array['02'] = 'Februari';
print_r($array);

echo $array[$month];

答案 4 :(得分:0)

您可以执行以下任何操作:

  1. if else

    if ($month == "01") {
        $dutchmonth = "Januari";
    } else if($month == "02"){
        $dutchmonth = "Februari";
    }
    
  2. <强>开关

    switch($month) {
        case "01":
            $dutchmonth = "Januari";
            break;
        case "02":
            $dutchmonth = "Februari";
            break;
    }
    
  3. 使用数组

    $month_arr = array('01' => "Januari", '02' => "Februari");
    $dutchmonth = $month_arr[$month];
    
  4. 注意:要使用多个if条件,请使用逻辑运算符 &amp;&amp; ||