How to work with global php variables
-
The following php snippet features a const, global variable and a shortcode function:
$some_var = 'one'; const SOME_CONST = 'two'; function shortcode_func() { global $some_var; return 'some_var=' . $some_var . ', SOME_CONST=' . SOME_CONST; } add_shortcode( 'shortcode_name', 'shortcode_func' );
The snippet is set as php, Auto Insert, Run Everywhere.
The shortcode function is called as per usual by placing [shortcode_name] inside the page markup.When the function is called, the output is:
some_var=, SOME_CONST=two
The const is recognized (showing that the snippet is executed as a whole, not just the function) but the global variable is not recognized. What gives?
To note, the global variable in my actual code is set in a different snippet and the code is more involved, so I’m not looking for a workaround to bypass using the global variable in this particular example. I want to know how to get global variables to work in WPCode, in general.
Thank you.
- The topic ‘How to work with global php variables’ is closed to new replies.