What is actually "encapsulate global variables within a function"

Hello Experts, I’m a bit confused with this. TF asked me to encapsulate global variables within a function . My all functions are declared as global and also enclosed.

Someone kindly clarify it . Please help :frowning:

Don’t declare global variables unless you do so inside a function:

function my_function() { global $my_var; $my_var = 'Hello world'; return $my_var; }

At least that’s my take on what that means.

Thanks for your kind reply!

Here is one of my functions:

`function kt_category(){
global $kt_option;
echo ‘’; the_category(’, '); echo ‘’;
}

`

Maybe they mean dont use global variables at al? Use a function call instead.

So instead of:

function kt_category(){
   global $kt_option;
   echo ''; the_category(', '); echo '';    
}

you would do:

function kt_category(){
   $kt_option = get_kt_option();
   echo ''; the_category(', '); echo '';    
}

(also $kt_option isn’t used in that particular function, so no need to even have it in there).

Many Thanks! I got it this time :slight_smile: