Okay So I know this is super basic and I should know how to do this but I’m blanking and having trouble finding the answer in Google. I have an include that has an array of variables for example
$phrases["text"][1] = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?";
$phrases["mp3"][1] = "http://example.com/file.mp3";
Then a function that gets the varibles:
function return_phrase($phrase_name="", $fallback="",$default ="text"){
$next= (isset($default) && $default =="mp3") ? 'text' : 'mp3';
if(isset($tts_phrases[$default][$phrase_name])){
return $phrases[$default][$phrase_name]);
}
else if(isset($tts_phrases[$next][$phrase_name])){
return $phrases[$next][$phrase_name]);
}
else{
return $fallback;
}
}
The problem is that the $phrases arrays aren’t being sent to the function I can include the file in the function itself but I know that’s the wrong way to do it. I think I need to use $global just not sure how.
Method 1: Pass $phrases, $tts_phrases as parameters
Method 2: Make $phrases, $tts_phrases global (bad!)
Using global variables is a quick and easy fix, but once your application gets larger they become very hard to keep track of. For example, take this legacy code snippet that I have to deal with at work:
Any time I am looking at one of the pages that just pulls one of those variables out of thin air, I have to Ctrl+F the whole project and make sure that every little change hasn’t messed up the whole app. When you keep your variables in local scope, you know exactly what you are changing.