Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6530651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:46:54+00:00 2026-05-25T09:46:54+00:00

How do I make this into a function? I’m doing the same thing twice…

  • 0

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") ) { // 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:46:54+00:00Added an answer on May 25, 2026 at 9:46 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make this code smaller into 1 block? $('#product INPUT:checked').each(function
I don't want to make this into a which is better... MVC or WebForms
I need to make this design: http://www.stephburningham.com/lmg/ into a joomla template but I've never
Before I get into the details of this problem, I'd like to make the
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
i'm lookin for a way to make some kind of function into php and
I'm just wondering why passing a System.Collections.Generic.List<string> into this function test(ICollection<object> t) will not
I want to make this script to work as LIVE() function. Please help me!
I am receiving errors and at the same time trying to make this work
I want to convert function object to function. I wrote this code, but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.