开发者

How to make this into PHP function [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

How do I make this into a function? I'm doing the same thing twice...

I tried to make the code as concise (given my newbie skills) as possible. If there's a better way to check if file exists with less code, or do anything with less code, please suggest it.

As you can see, I need to 1) check the file exists 2) make it night, if it's night 3) check if that exists 4) get the key – for some reason, the incoming text will NOT MATCH string key 100%, so that's why I'm using string match instead of =

$today_desc_stripped = trim($today_desc);
foreach ( $icon as $k => $v ) {
similar_text($today_desc_stripped, $k, $p);
if ( $p > 90) {
    $today_desc = $k;
    break;
}   
}
$today_icon = $icon[$today_desc];
// if it's past 6 pm, set temp icon to night icon
if ( file_exists("scripts/weather_icons/".$today_icon.".png") ) 开发者_Go百科{ // make sure file exists
if ( $time >= 6 ) {
    $temp = $today_icon."_night";
    if ( file_exists($temp) ) {
        $today_icon = $temp;
    }
}
} else {
//if file doesn't exist
$today_icon = "dunno";
mail($to,$subject,$message."195: Icon array on Line 20 is missing assignment for ".$today_desc_stripped);
}

$fourday_desc_stripped = trim($fourday_desc);
foreach ( $icon as $k => $v ) {
similar_text($fourday_desc_stripped, $k, $p);
if ( $p > 90) {
    $fourday_desc = $k;
    break;
}   
}
$fourday_icon = $icon[$fourday_desc];
// if it's past 6 pm, set temp icon to night icon
if ( file_exists("scripts/weather_icons/".$fourday_icon.".png") ) { // make sure file exists
if ( $time >= 6 ) {
    $temp = $fourday_icon."_night";
    if ( file_exists($temp) ) {
        $fourday_icon = $temp;
    }
}
} else {
//if file doesn't exist
$fourday_icon = "dunno";
mail($to,$subject,$message."218: Icon array on Line 20 is missing assignment for ".$fourday_desc_stripped);
}


Here's the steps that you need to follow:

  • Identify the goal
  • Identify the differences
  • Identify supporting data

You can either put your error checking inside the function or outside. As there were several variables that were set up in our current scope simply for your error email, I left it outside of the function:

$today_icon = getIcon($icon, $today_desc);
if (!$today_icon) {
    mail($to,$subject,$message."195: Icon array on Line 20 is missing assignment for ".$today_desc);
}

$fourday_icon = getIcon($icon, $fourday_desc);
if (!$fourday_icon) {
    mail($to,$subject,$message."195: Icon array on Line 20 is missing assignment for ".$fourday_desc);
}

And for the function itself:

function getIcon($icons, $icon_name_request) {
    $icon_name = trim($icon_name_request);

    foreach ( $icons as $k => $v ) {
        similar_text($icon_name, $k, $p);
        if ( $p > 90) {
            $icon_name = $k;
            break;
        }   
    }

    $icon_filebase = null;

    // if it's past 6 pm, set temp icon to night icon
    if ( !empty($icons[$icon_name]) && 
        file_exists("scripts/weather_icons/". $icons[$icon_name] .".png") ) {
                 // make sure file exists

        $icon_filebase = $icons[$icon_name];

        if ( $time >= 6 ) {
            $temp = $icon_filebase."_night";
            if ( file_exists($temp) ) {
                $icon_filebase = $temp;
            }
        }
    }
    return $icon_filebase;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜