根据第一个变量的输出将输出保存到新变量

I have some code that saves xml content to variables. One of those variables can be a number that ranges from 1-240. Based on a range (like 1-30, 30-60, 60-90, etc), I want to save some text to a new variable.

My current code is having issues (I'm fairly new to php, but catch on quick). For instance, my saved variable from the xml document ($storm_wind) was 100, which according to my code, should have saved $category variable as 2. However, it saves the variable as 5 (which I'm assuming is because my last line in the code is $category = '5';. What is the proper way to execute this code? Should I be using elseif statements instead?

Below is a snippet of the code that is currently not working:

$wind_value = $xml->channel->item[0]->nhcCyclone->nhcwind;
$storm_wind = substr($wind_value, 0, -4);

if(($storm_wind >=1) && ($storm_wind <=38); { 
    $category = 'TD'; }
if($storm_wind >=39 && $storm_wind <=73); {
    $category = 'TS'; }
if($storm_wind >=74 && $storm_wind <=95); {
    $category = 1; }
if($storm_wind >=96 && $storm_wind <=110 ); {
    $category = 2; }
if($storm_wind >=111 && $storm_wind <=129 ); {
    $category = 3; }
if($storm_wind >=130 && $storm_wind <=156 ); {
    $category = 4; }
if($storm_wind >=157 && $storm_wind <=240 ); {
    $category = 5; }

Remove the semicolons that are terminating your if constructs early, e.g.:

if($storm_wind >=1 && $storm_wind <=38); { 
    $category = 'TD'; }

Should be:

if($storm_wind >=1 && $storm_wind <=38) { 
    $category = 'TD'; }

and so on for all of them.


What's happening in your code is that an if construct can take the form of:

if(expression) statement;

without curly braces for a single statement - and the statement can be empty. So

if(expression);

is itself a complete, isolated construct. You just happen to be following each with an unrelated statement block wrapped in braces. So each of your if constructs are doing nothing, and $category is being set to each value in turn, leaving it at 5 at the end.

Although the question has already been answered by Paul, yes, you could use elseif to reduce the number of statements executed:

if ($storm_wind >=1 && $storm_wind <=38) { 
    $category = 'TD'; 
} elseif ($storm_wind >=39 && $storm_wind <=73) {
    $category = 'TS'; 
} elseif ($storm_wind >=74 && $storm_wind <=95) {
    $category = 1; 
} elseif ($storm_wind >=96 && $storm_wind <=110) {
    $category = 2;
} elseif ($storm_wind >=111 && $storm_wind <=129) {
    $category = 3; 
} elseif ($storm_wind >=130 && $storm_wind <=156) {
    $category = 4; 
} elseif ($storm_wind >=157 && $storm_wind <=240) {
    $category = 5; 
}

Alternatively, you could extract a function and return early:

function stormCategoryFrom($windSpeed) 
{
    if ($windSpeed >= 157) {
        return 5;
    }

    if ($windSpeed >= 130) {
        return 4;
    }

    if ($windSpeed >= 111) {
        return 3;
    }

    if ($windSpeed >= 96) {
        return 2;
    }

    if ($windSpeed >= 74) {
        return 1;
    }

    if ($windSpeed >= 39) {
        return 'TS';
    }

    return 'TD';
}

Or:

function stormCategoryFrom($windSpeed) 
{
    $categories = [
        157 => 5,
        130 => 4,
        111 => 3,
        96 => 2,
        74 => 1,
        39 => 'TS',
    ];

    foreach ($categories as $minimumSpeed => $category) {
        if ($windSpeed >= $minimumSpeed) {
            return $category;
        }
    }

    return 'TD';
}

Also, looking at https://en.m.wikipedia.org/wiki/Tropical_cyclone_scales#Comparisons_across_basins it appears you are off by one.