为什么这个if语句返回true?

时间:2018-05-25 09:10:24

标签: php

为什么这会回归真实? Party Bounce和Slide 13 x18不在if语句中?

secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

快速解决方案,更易于管理,甚至更重要..阅读。

$attributes = [
    "12x15 Purple and Yellow 2", 
    "11x13 Red and Yellow 1", 
    "12x15 Yellow and Purple old",
    "11x13 Red and Yellow 2", 
    "12x15 Purple and Yellow 1"
];

$types = [
    "Bootcamp Obstacle Course",
    "Terminator Torment Obstacle Course",
    "Lizard Obstacle Course",
    "Bugs Obstacle Course",
    "Nemo Obstacle Course"
];

if(array_intersect($btypes, $attributes) !== [] && array_intersect($btypes, $types) !== []){
    return "Standard Bouncy Castle and an Obstacle Course";
}

答案 1 :(得分:1)

"12x15 Purple and Yellow 2" || "11x13 Red and Yellow 1"是一个逻辑表达式。在布尔上下文中,除"""0"之外的任何字符串都是evaluated as TRUE。上面的表达式与TRUE || TRUE具有相同的值,其值为TRUE

默认情况下,in_array()使用==将其第一个参数与作为第二个参数传递的数组的每个元素进行比较。

您传递给in_array()的第一个参数为TRUE,而==""的任何字符串都为"0"$btypes中的所有字符串均为==TRUEin_array()返回TRUE

了解PHP compares values of different typeshow different values behave when they are compared using == and ===

的方式

根据您要实现的目标,建议您使用array_intersect()和/或array_diff()来获取$btypes中也存在的值。

$sizes = [
    '12x15 Purple and Yellow 2',
    '11x13 Red and Yellow 1',
    '12x15 Yellow and Purple old',
    '11x13 Red and Yellow 2',
    '12x15 Purple and Yellow 1',
];
$courses = [
    'Bootcamp Obstacle Course',
    'Terminator Torment Obstacle Course',
    'Lizard Obstacle Course',
    'Bugs Obstacle Course',
    'Nemo Obstacle Course',
];

$btypes = [
    '11x13 Red and Yellow 2',
    'Party Bounce and Slide 13 x 18',
];

if (count(array_intersect($sizes, $btypes)) &&
    count(array_intersect($courses, $btypes))) {
    // At least one size and at least one course are present in $btypes
    return "Standard Bouncy Castle and an Obstacle Course";
}

答案 2 :(得分:0)

重新格式化代码可以更轻松地按照此处的操作进行操作:

<?php
function is_bouncy_obstacle() {
    $btypes = [
        '11x13 Red and Yellow 2',
        'Party Bounce and Slide 13 x 18'
    ];
    if(
        in_array(
            "12x15 Purple and Yellow 2" || 
            "11x13 Red and Yellow 1" || 
            "12x15 Yellow and Purple old" || 
            "11x13 Red and Yellow 2" || 
            "12x15 Purple and Yellow 1"
            , 
            $btypes
        ) && 
        in_array(
            "Bootcamp Obstacle Course" ||  
            "Terminator Torment Obstacle Course" || 
            "Lizard Obstacle Course" || 
            "Bugs Obstacle Course" || 
            "Nemo Obstacle Course"
            ,
            $btypes
        )
    )
    {
        return "Standard Bouncy Castle and an Obstacle Course";
    }
}

var_dump(is_bouncy_obstacle());

输出:

string(45) "Standard Bouncy Castle and an Obstacle Course"

所以它返回一个字符串,而不是true。但是如果你在比较中使用上面的字符串就认为是真的。

重要的是in_array有两个参数:针和大海捞针。你正在寻找大海捞针。论证顺序很重要。

使用in_array

的示例
var_dump(in_array('apple', ['apple', 'orange', 'pear']));
var_dump(in_array('apple', ['banana', 'papaya', 'sultana']));

输出:

bool(true)
bool(false)

返回您的代码。

考虑:

var_dump('foo');
var_dump('foo' || 'bar');
var_dump(in_array('foo' || 'bar', ['baz']));
var_dump(in_array(true, ['qux']));

输出:

string(3) "foo"
bool(true)
bool(true)
bool(true)

最后感觉有点奇怪!这是因为松散的比较是in_array的默认值。

你需要重写你的条件。