Jump to content

question on if statement


mjb982

Recommended Posts

I'm writing a code that pulls current weather conditions from the NWS xml data. The problem I'm running into is I would like custom weather icons, not the default NWS ones.

 

I can accomplish this by using individual if statements for each weather description, ie:

 

if($current['weather']=="Partly Cloudy"){

$icon = "images/pcloudy.png";

}

 

But there are hundreds of them, ie "A Few clouds", "a few clouds and windy" etc... I was wondering if there was a way that I'm not familiar with do make an if statement where if "current['weather']" contiains the words "cloud" then I could display the correct image.

 

That would cut down the time it takes, and make things so much simpler. Something like

 

if($current['weather'] contains "cloud"){

$icon = "images/clouds.png"'

}

 

is it possible?

Link to comment
https://forums.phpfreaks.com/topic/175146-question-on-if-statement/
Share on other sites

I would use str_pos() which will tell you if your string is part of the xml data

http://us.php.net/manual/en/function.strpos.php

 

<?php
$pos = str_pos('cloud', $current['weather']);
if($pos !== false){
     $icon = "images/clouds.png";
}
?>

 

Personally I dont like to use ==, !=, etc on strings but stick to the string comparision functions available (see strcmp() etc)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.