Can someone help me simplify this code? Is there a way I can permanently pass $the_campus to is_campus() without using a global scope? $the_campus is pulling from a database and the value changes, but the variable is always called $the_campus.
Thanks!
$the_campus = $search_term['campus'];
function is_campus($the_campus, $selected_campus) {
if ( $selected_campus == $the_campus ) {
echo 'selected';
}
}
<?php is_campus($the_campus, 'university-of-minnesota-tc'); ?>
<?php is_campus($the_campus, 'university-of-wisconsin'); ?>
<?php is_campus($the_campus, 'university-of-chicago'); ?>
You might devise a way of passing the variable without explicitly writing
globalin your function, but please do not. The function is just fine as it is; introducing “invisible” coupling with a variable is going to make your code worse, not better.What you would probably want to do is refactor the code so that it works in an equivalent manner but is more straightforward to use, for example: