Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A better way for such cases? 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys,

There is something that I often wonder about the way to use custom functions :
When the output of a function is used for a condition and for a result at the same time, how do you make it so that the function is run only once?

I mean, in the example below, is there a way to produce the same result but with hd_file_format_check run only once?

Thanks ! ;)

Code:
function hd_file_format_check($file_type, $ok_types) {

$types_array = explode("|", $ok_types);

$result = false;

    for ($k = 0; $k < sizeof($types_array); $k++) {
    
        if (strstr($file_type, $types_array[$k])) {
        
        $result = "." . str_replace("jpeg", "jpg", $types_array[$k]);
        
        }

    }
    
return $result;

}


--------------------------

..

if(hd_file_format_check($_FILES["form_prod_pic"]["type"][$key], "gif|jpeg|jpg") != false) {

$file_ext = hd_file_format_check($_FILES["form_prod_pic"]["type"][$key], "gif|jpeg|jpg");

// do other things here ...

}

..
 
Code:
if (($result = hd_file_format_check(somedata) !== false){

// do something good with $result
}else{
echo "wrong file format";
}

notice the positioning of the round brackets in the if.

 
mistake
Code:
if (($result = hd_file_format_check(somedata)[red])[/red] !== false){
 

That's great! :)
I was sure there was a way!

Thanks x1000 ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top