为什么这个if语句失败了两个参数

时间:2015-05-04 12:12:06

标签: php

我包含一大块html,其中包含一些基于if语句的echo语句。

<?
    if($rows['business_type'] !== "Hotel" || "Bed & Breakfast")
    {
?>


    <div id="mini_titles"><h2>Opening times</h2></div>

    <div id="complete_info_wrapper">
            <div id="complete_info_title"><p>Monday</p></div>
            <div id="complete_info_details_opening">
               <p>
<?php echo $rows1['opening_monday_from']; ?> - 
<?php echo $rows1['opening_monday_to']; ?>
               </p>
           </div>
    </div>
    <div id="complete_info_wrapper">
        <div id="complete_info_title"><p>Tuesday</p></div>
        <div id="complete_info_details_opening">
            <p>
<?php echo $rows1['opening_tuesday_from']; ?> - 
<?php echo $rows1['opening_tuesday_to']; ?>
            </p>
        </div>
        </div>

        <div id="complete_info_wrapper">
            <div id="complete_info_title"><p>Wednesday</p></div>
            <div id="complete_info_details_opening">
               <p>
<?php echo $rows1['opening_wednesday_from']; ?> - 
<?php echo $rows1['opening_wednesday_to']; ?>
               </p>
            </div>
        </div>

        <div id="complete_info_wrapper">
            <div id="complete_info_title"><p>Thursday</p></div>
            <div id="complete_info_details_opening">
                <p>
<?php echo $rows1['opening_thursday_from']; ?> - 
<?php echo $rows1['opening_thursday_to']; ?>
                </p>
            </div>
        </div>

        <div id="complete_info_wrapper">
            <div id="complete_info_title"><p>Friday</p></div>
            <div id="complete_info_details_opening">
               <p>
<?php echo $rows1['opening_friday_from']; ?> - 
<?php echo $rows1['opening_friday_to']; ?>
               </p>
            </div>
        </div>

        <div id="complete_info_wrapper">
            <div id="complete_info_title"><p>Saturday</p></div>
            <div id="complete_info_details_opening">
                <p>
<?php echo $rows1['opening_saturday_from']; ?> - 
<?php echo $rows1['opening_saturday_to']; ?>
                </p>
           </div>
        </div>

        <div id="complete_info_wrapper">
            <div id="complete_info_title"><p>Sunday</p></div>
            <div id="complete_info_details_opening">
                <p>
<?php echo $rows1['opening_sunday_from']; ?> - 
<?php echo $rows1['opening_monday_to']; ?>
                </p>
            </div>
        </div>
<?php
    }

?>

if if statment如果我只使用一个ie:

if($rows['business_type'] !== "Hotel"){}

但是,只要我添加||or&&and,第二个就会失败,无论如何都会显示。

我确定它缺少一些愚蠢而简单的东西。

请帮助,谢谢。

6 个答案:

答案 0 :(得分:4)

如果条件为$rows['business_type'] !== "Hotel"为真,那么条件将始终为真。因为if ("Bed & Breakfast")true。因此true || true将是true

应该是 -

if($rows['business_type'] !== "Hotel" || $rows['business_type'] != "Bed & Breakfast")

或者你可以做 -

if (!in_array($rows['business_type'], array("Hotel", "Bed & Breakfast")))

答案 1 :(得分:0)

尝试:

if($rows['business_type'] !== "Hotel" || $rows['business_type'] !== "Bed & Breakfast")

答案 2 :(得分:0)

因为它有一些拼写错误:

更改

<?
if($rows['business_type'] !== "Hotel" || "Bed & Breakfast")
 // This //statement will always return true, because even if first condition 
// satisfies or not, 'Bed & Breakfast' will always return true (it is a 
//string which is not empty.
{
?>
// TRUE || TRUE --> TRUE
// FALSE || TRUE --> TRUE
// TRUE || FALSE --> TRUE
// FALSE || FALSE --> FALSE

<?
if($rows['business_type'] !== "Hotel" || $rows['business_type'] !== "Bed & Breakfast") // PUT YOUR CONDITION CORRECTLY.
{
?>

答案 3 :(得分:0)

您无法通过这种方式与两个值进行比较。使用in_array

if (!in_array($rows['business_type'], array("Hotel", "Bed & Breakfast"))) {...}

或使用||

if ($rows['business_type'] !== "Hotel" || $rows['business_type'] !== "Bed & Breakfast") {...}

答案 4 :(得分:0)

“Bed&amp; Breakfast”是一个不是比较的字符串,它总是返回true。因此x or true始终等于true。那是你的if语句的问题。 这可能会按预期工作:

if($rows['business_type'] !== "Hotel" || $rows['business_type'] !== "Bed & Breakfast")

答案 5 :(得分:0)

尝试数组方法

if (in_array(array('Hotel', 'Bed'), $rows['business_type'])) {
    echo "'Bed' was found\n";
}