即使所需的值不存在,我的php的if语句也会通过

时间:2016-04-21 16:24:05

标签: php if-statement

我正在尝试创建一个从早上8点到下午4点的预订系统,我故意将我最近的数据库日期设置为:2016-04-23 17:50:00这将转换为一小时5分钟和50分钟但不知何故,即使我的if语句只接受一小时的8,9,10,11,1,2,3值,它仍然设法以5小时进入。

我的代码是:

require 'spec_helper'

    describe Zipadmin::LocationController, type: :controller do
     before(:all) do
       @admin = create(:admin)
       @location = create(:location)
     end

    after(:all) do
      @admin.destroy
      @location.destroy
    end

    let(:admin) {@admin}
    let(:location) {@location}

    before(:each) do
      sign_in(:user, admin)
    end

    describe '#create' do
      it 'creates a new location' do

        expect{
          post :create, location:{
            name: 'sample location',
            phone: '5555555555',
            fax: '5555555555',
            location_id: '123456',

            address_attributes: {
              address1: '12345 Some St',
              city: 'Portland',
              state: 'OR',
              zip: '91237'
            }
          }
        }.to change{Location.count}.by(1)

        new_location = Location.last
        expect(new_location.name).to eq 'sample location'
        expect(new_location.phone).to eq '5555555555'
        expect(new_location.fax).to eq '5555555555'
        expect(new_location.ncpdp_id). to eq '123456'
        expect(new_location.address.address1).to eq '12345 Some St'
        expect(new_location.address.city).to eq 'Portland'
        expect(new_location.address.state).to eq 'OR'
        expect(new_location.address.zip).to eq '91237'
       end
     end
    end

我尝试通过放置回声进行故障排除,以确定我的ifs被穿透的位置,结果如下:

$get_recent_date = mysqli_fetch_assoc(mysqli_query($con,"SELECT date FROM schedule ORDER BY date desc"));
$get_datetime = new DateTime($get_recent_date['date']);
$date = date_format($get_datetime, 'j'); //numeric date w/o leading zeros
$month = date_format($get_datetime, 'n'); //numeric month w/o leading zeros
$year = date_format($get_datetime, 'Y'); //numeric year w/o leading zeros
$hour = date_format($get_datetime, 'g'); //numeric hour w/o leading zeroes
$minute = date_format($get_datetime, 'i'); //numeric hour w/o leading zeroes

echo "Hour is: "; echo $hour; echo "<br>";
echo "Minute is: "; echo $minute; echo "<br>";

if ($hour == "8" || "9" || "10" || "11" || "1" || "2" || "3")
{
    if($minute == "0" || "00" || "10" || "20" || "30" || "40")
    {
        echo "Minute before adding ten is: "; echo $minute; echo "<br>";
        $minute = $minute + '10';
        echo "New Minute is: "; echo $minute; echo "<br>";
            if($minute == "60")
            {
                echo "Wtf, why did it go here?";
            }
    }

    else if($minute == "50")
    {
        $hour = $hour + '1';
        $minute = "0";

        $date = strtotime($date);
        $hour = strtotime($hour);
        $minute = strtotime($minute);
        echo $date; echo $hour; echo $minute;
        $got_datetime = date_create_from_format('j-n-Y-g-i', "$date-$month-$year-$hour-$minute");
        mysqli_query($con,"INSERT INTO schedule (date) VALUES ($got_datetime)");
    }

    else
    {
        echo "Minute is incorrect";
    }

}


else {
    echo "Hour is incorrect";
}

(遗憾的是,由于我是新用户,我无法发布照片)

1 个答案:

答案 0 :(得分:1)

if ($hour == "8" || "9" || "10" || "11" || "1" || "2" || "3")

应该是

if ($hour == "8" || $hour == "9" || $hour == "10" || $hour == "11" || $hour == "1" || $hour == "2" || $hour == "3")

其他地方也一样。

字符串“9”将评估为布尔值“true”,因此($ hour ==“8”||“9”)与写入相同($ hour ==“8”|| true) - 或者只是“真实”。